php data to html form
Moderator: General Moderators
php data to html form
Hi helpful folks!
I have an html form. The data is validated, calculated in an external php file. Once the php file is done, I would like to have the resulting value returned to the html form. Is there a way to do this?
Thanks!
I have an html form. The data is validated, calculated in an external php file. Once the php file is done, I would like to have the resulting value returned to the html form. Is there a way to do this?
Thanks!
Re: php data to html form
I'm not 100 sure what's going on.
Do you have a PHP file that is generating the data you want to populate the form with & you're wondering how to get the data (generated by PHP) into the form?
Is the HTML form currently generated by PHP as well?
Do you have a PHP file that is generating the data you want to populate the form with & you're wondering how to get the data (generated by PHP) into the form?
Is the HTML form currently generated by PHP as well?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: php data to html form
Sort of.
I have an html file with the form. I enter some values. Submitting the form calls the validate.php file. The php file checks the values, performs the necessary calculations.
Then I would like this calculated value to appear in the original html file. Not necessarily in the form, but somewhere in the original html file.
I have an html file with the form. I enter some values. Submitting the form calls the validate.php file. The php file checks the values, performs the necessary calculations.
Then I would like this calculated value to appear in the original html file. Not necessarily in the form, but somewhere in the original html file.
Re: php data to html form
Ah, ok.
If I was in your position, I'd rethink your setup a bit. Have your form & validation functions all accessible from one URL (say: myform.php). When someone first views the page, they get the form. When they submit the form, it POSTs (or GETs - doesn't really matter) the data to itself, which then validates the data (you can include() validate.php to do this in order to keep things clean). validate.php can then set variables that you can display later in the generated HTML.
If you necessarily want to keep the order of pages you've got, you can use header() to forward the user back to your form. In the URL that you place in your header() call, include some GET variables, which your form can then (it'll still have to be a *.php file) display those GET variables.
If I was in your position, I'd rethink your setup a bit. Have your form & validation functions all accessible from one URL (say: myform.php). When someone first views the page, they get the form. When they submit the form, it POSTs (or GETs - doesn't really matter) the data to itself, which then validates the data (you can include() validate.php to do this in order to keep things clean). validate.php can then set variables that you can display later in the generated HTML.
If you necessarily want to keep the order of pages you've got, you can use header() to forward the user back to your form. In the URL that you place in your header() call, include some GET variables, which your form can then (it'll still have to be a *.php file) display those GET variables.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: php data to html form
Perhaps it would be more simple to just have your form submit to "?submit=true" (the same page, but with submit=true in the $_GET array).
Then, try this code where you want the result to display:
Then, try this code where you want the result to display:
Code: Select all
<?php
if($_GET['submit']){
include('validator.php');
}else{
echo "Something went wrong.";
}
?>
Re: php data to html form
OK, I went along with the following setup:
My only question now is, how to display the table once the form is submitted?
Code: Select all
if ($_POST['submit'])
{
include('validate.php');
// refresh page
}
else
{
// display the table
}Re: php data to html form
since you are using a post, and not a get, your action should be "?" (the source file)
and you need a hidden submit input:
and you need a hidden submit input:
Code: Select all
<input type="hidden" name="submit" value="true">Re: php data to html form
I posted recently HERE about forms, so I might as well pimp it to you in case it helps.
I was also going to suggest that you not rely on the submit button triggering form action. I believe it's not technically required for a browser to pass submit button values back to the server, so your users could wind up with a form that is impossible to submit. The hidden field is the way to go there.
As for displaying the value, you could use a hidden div with a field you can set the value for ...
... or just output it with
Hope that helps.
I was also going to suggest that you not rely on the submit button triggering form action. I believe it's not technically required for a browser to pass submit button values back to the server, so your users could wind up with a form that is impossible to submit. The hidden field is the way to go there.
As for displaying the value, you could use a hidden div with a field you can set the value for ...
Code: Select all
<input type="textbox" name="result" value="<?php echo $calculated_value; ?>"Code: Select all
<?php echo $calculated_value; ?>Re: php data to html form
Thanks for the help guys.
I know how to output the result. My problem is, I would like to display the form along with the output. Reason is, if the user is not satisfied with the result, they can make changes (enter a new value and such).
But in my script, I check if the form was submitted; if yes, the calculations are performed; if not, the form is displayed. Do I have to include the form generation part in both the "yes" branch and the "no" branch?
I know how to output the result. My problem is, I would like to display the form along with the output. Reason is, if the user is not satisfied with the result, they can make changes (enter a new value and such).
But in my script, I check if the form was submitted; if yes, the calculations are performed; if not, the form is displayed. Do I have to include the form generation part in both the "yes" branch and the "no" branch?
Re: php data to html form
Or get rid of your yes & no branches altogether. Make the validate.php inclusion conditional on the form being submitted & that's it. Don't make the form output conditional - output it every time.
Edit: The clicked submit button must be submitted along with the form: http://www.w3.org/TR/html401/interact/f ... mit-format
Edit: The clicked submit button must be submitted along with the form: http://www.w3.org/TR/html401/interact/f ... mit-format
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: php data to html form
"...get rid of your yes & no branches altogether"
My goodness, simplicity escaped me
Thanks for all your help!
My goodness, simplicity escaped me
Thanks for all your help!
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: php data to html form
Check the request method and the value of the $POST array. You don't need to pass flags all over the place in either GET or POST. Submit buttons are not always passed so don't use them as your check (think Microsoft and their complete lack of adherence to the standards).
Your form should have default null values set so if there is no post the nulls echo out in the vars. If there is a post, use the validator code your wrote to push those values back to the form, resetting the defaults.
Your form should have default null values set so if there is no post the nulls echo out in the vars. If there is a post, use the validator code your wrote to push those values back to the form, resetting the defaults.
Code: Select all
<?php
$name = $age = null;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Because seriously, you only need to validate posted data
require_once 'validate.php';
// Assumes a validate function that takes an array and returns it with valid data in each passed key
$post = validate($_POST);
// Reset the default values to be shown in the form
$name = $post['name'];
$age = $post['name'];
// Perhaps use this space to do stuff with the posted form data
// And make sure to give your users an indication that the form was processed
}
?>
<form method="post" action="<?php echo basename(__FILE__) ?>">
Name: <input type="text" name="name" value="<?php echo $name ?>" /><br />
Age: <input type="text" name="age" value="<?php echo $age ?>" /><br />
<input type="submit" name="form-submit-button-to-not-use-for-form-posting-checks" value="Post this" />
</form>
Re: php data to html form
Hey ... nice solution on catching form submissions. I learned that the hard way by making a nice little app with a few forms, only to realise that an apparently random set of IE users couldn't use the forms. Grrr. This is a nice clean approach.
*brain updated*
*brain updated*