Page 1 of 1

php data to html form

Posted: Thu Jul 17, 2008 5:47 pm
by hpisti
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!

Re: php data to html form

Posted: Thu Jul 17, 2008 5:51 pm
by pickle
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?

Re: php data to html form

Posted: Thu Jul 17, 2008 5:53 pm
by hpisti
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.

Re: php data to html form

Posted: Thu Jul 17, 2008 5:58 pm
by pickle
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.

Re: php data to html form

Posted: Thu Jul 17, 2008 6:22 pm
by omniuni
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:

Code: Select all

 
 
<?php
 
if($_GET['submit']){
include('validator.php');
}else{
echo "Something went wrong.";
}
 
?>
 
 

Re: php data to html form

Posted: Thu Jul 17, 2008 6:36 pm
by hpisti
OK, I went along with the following setup:

Code: Select all

if ($_POST['submit'])
{
  include('validate.php');
  // refresh page
}
else
{
  // display the table
}
My only question now is, how to display the table once the form is submitted?

Re: php data to html form

Posted: Thu Jul 17, 2008 7:40 pm
by omniuni
since you are using a post, and not a get, your action should be "?" (the source file)

and you need a hidden submit input:

Code: Select all

<input type="hidden" name="submit" value="true">

Re: php data to html form

Posted: Thu Jul 17, 2008 7:59 pm
by Stryks
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 ...

Code: Select all

<input type="textbox" name="result" value="<?php echo $calculated_value; ?>"
... or just output it with

Code: Select all

<?php echo $calculated_value; ?>
Hope that helps.

Re: php data to html form

Posted: Fri Jul 18, 2008 9:36 am
by hpisti
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?

Re: php data to html form

Posted: Fri Jul 18, 2008 9:44 am
by pickle
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

Re: php data to html form

Posted: Fri Jul 18, 2008 11:11 am
by hpisti
"...get rid of your yes & no branches altogether"
My goodness, simplicity escaped me 8O

Thanks for all your help!

Re: php data to html form

Posted: Fri Jul 18, 2008 3:09 pm
by RobertGonzalez
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.

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

Posted: Fri Jul 18, 2008 7:32 pm
by Stryks
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* 8)