Jump to content

How To Post Data From A Php Program To Fields In A Form


creacon

Recommended Posts

I need to be able to pass data BACK to a form on a page. I tried $_POST['fieldname'] = "Joe E Blow" (using a literal just for testing purposes), but that didn't work. The php script is at the very top of the form (I was told that's where it should be to execute each time the page loads). The purpose is to ultimately post data that has been passed from a form on a preceeding page, and load it into fields in a form on this page.Maybe my whole approach is wrong; can anyone tell me the proper way to do this?

Link to comment
Share on other sites

The $_POST array (like the $_GET array) is populated by the PHP engine at runtime. Its values come from data that is SENT TO your script, either in a url query string or through a form submission. They are technically read/write variables, but they are mostly for reading. Writing data into $_POST does not send it back to the browser.To write any data to a browser, we mostly use the echo statement.You can assign "Joe Blow" to any global variable and then access it anywhere on your page. Let's say you had this statement at the top of your page:$person = "Joe Blow";You can do this, or something like it, in the middle of your form:<input type="text" value="<?php echo $person; ?>">The HTML gets read by the PHP parser, and when it hits the <?php ?> tag, it executes the PHP inside. You can have tags like that all over your HTML.

Link to comment
Share on other sites

In order to get the data back to the form, you can save it in the $_SESSION[] variable array, so you can read it later. For example, if you set$_SESSION["name"] = $_POST["name"] then when you redirect back to the page with the form, you can have the value of the fields set to something like this:<input type="text" id="name" value="<?php echo ($_SESSION["name"]?$_SESSION["name"]:""); ?> />That is just the same as Deirdre's Dad's solution, but it explains where the value comes from, I think. The syntax is just shorthand forif ($_SESSION["name"]) {echo $_SESSION["name"]} else { echo "" }Edit: two things I forgot: you need to have session_start() at the top of each page between <?php ?> tags for that to work, and when you are finished setting the session variables, you have to write session_write_close() for it to stick.

Link to comment
Share on other sites

The $_POST array (like the $_GET array) is populated by the PHP engine at runtime. Its values come from data that is SENT TO your script, either in a url query string or through a form submission. They are technically read/write variables, but they are mostly for reading. Writing data into $_POST does not send it back to the browser.To write any data to a browser, we mostly use the echo statement.You can assign "Joe Blow" to any global variable and then access it anywhere on your page. Let's say you had this statement at the top of your page:$person = "Joe Blow";You can do this, or something like it, in the middle of your form:<input type="text" value="<?php echo $person; ?>">The HTML gets read by the PHP parser, and when it hits the <?php ?> tag, it executes the PHP inside. You can have tags like that all over your HTML.
I Placed the "$person" script at the top of the form, just above the first <html? tag (i.e. <?php $person = "Joe E. Blow"). Then I cut/pasted the value= from above to the form, EXACTLY as above. When I run the page the text field displays the php echo line (i.e. the text field contains literally: <?php echo $person; ?>). Please tell me what I'm doing wrong??????
Link to comment
Share on other sites

Usually, when you can see the PHP tags, it's because PHP isn't installed on your server. I would check on that first.
Thank you for the heads-up thought, but yes I do have PHP installed with my local server (I'm using XAMPP), and have been using it on another page set. It's also on my remote server (Yahoo web hosting). Here's the code on the problem page:The following is at the top of the page, just before the <html> tag.
<!--$templateKey E-commerce|Techy Lines - Steel Blue|2.0$-->   <?php       $fullname="Joe E. Blow";           // This was just to see if this code ever executed, but it didn't show up anywhere on the page.       echo "$fullname";          ?> <html>  <head>      <title>Home</title>

Could it be that the above is in the wrong place on the page? If so, then where should it be. One of my biggest confusions is where to physically put the php code.The following is the form (only one field):

<div id="apDiv1">WOTC pre-Questionnaire for:  <p>    <form name="topper" >      <input type="text" value="<? echo $fullname; ?>">    </form>  </div>  <div id="apDiv3"></div>  </body></html>

When the page was displayed, the "name" field contained: <? echo $fullname; ?>In fact, it even shows there in my Dreamweaver design view. If I remove it, it removes the code.I hope there's some glaring error up there that you can tell me about, so I can get this thing to work. It seems that everything I try here doesn't work, even if I copy it right out of a book. After 50 years of progrmming, web programming is becoming the most frustrating for me, but I'm determined to learn it if it kills me (and at my age, that may not be far off) LOL.

Link to comment
Share on other sites

I'm not sure if it was mentioned yet, but make sure the file is named with a .php extension also. You can configure the server to send any extension to the PHP interpreter, but by default it will only send .php files (and maybe .php3 for legacy).

Link to comment
Share on other sites

I'm not sure if it was mentioned yet, but make sure the file is named with a .php extension also. You can configure the server to send any extension to the PHP interpreter, but by default it will only send .php files (and maybe .php3 for legacy).
WOW! Pin the "Stupid" badge on me and initiate me into the idiot club. Somehow, in all those explanations, I failed to pick up on the fact that I had to rename the page(s) from .html to .php. Talk about egg on my face; I think it's an Ostrich egg. That was the WHOLE PROBLEM.Thanks everyone for your kind assistance.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...