php data to html form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hpisti
Forum Newbie
Posts: 5
Joined: Thu Jul 17, 2008 5:45 pm

php data to html form

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: php data to html form

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
hpisti
Forum Newbie
Posts: 5
Joined: Thu Jul 17, 2008 5:45 pm

Re: php data to html form

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: php data to html form

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: php data to html form

Post 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.";
}
 
?>
 
 
hpisti
Forum Newbie
Posts: 5
Joined: Thu Jul 17, 2008 5:45 pm

Re: php data to html form

Post 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?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: php data to html form

Post 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">
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: php data to html form

Post 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.
hpisti
Forum Newbie
Posts: 5
Joined: Thu Jul 17, 2008 5:45 pm

Re: php data to html form

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: php data to html form

Post 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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
hpisti
Forum Newbie
Posts: 5
Joined: Thu Jul 17, 2008 5:45 pm

Re: php data to html form

Post by hpisti »

"...get rid of your yes & no branches altogether"
My goodness, simplicity escaped me 8O

Thanks for all your help!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: php data to html form

Post 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>
 
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: php data to html form

Post 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)
Post Reply