Jump to content

How Do I Get Php Processed Form Data To Echo On The Same Web Page As The Form?


confused and dazed

Recommended Posts

Hello internet. I have been sending my site out to people to test out and mess around with and some feedback I am getting is that they are having pop-up blocker issues and even when they temporarily turn the pop-up blockers off they still do not get my echo's that I really need them to get. So - how do I send the echo right to the same webpage that the form is on?

Link to comment
Share on other sites

pop up blocker will block some js action which cause pop ups. but there is nothing to do it with php. once a page has been served its out of scope of php. echo should show the data as it should be,at the time of page serving. can you tell more about what are you trying to do actualy? or codes?

Link to comment
Share on other sites

have the form submit to itself. i.e. the action and the page with the form will be the same. Just make sure to check for submit in GET/POST so you don't get errors trying to echo out values when the haven't been set yet.

Link to comment
Share on other sites

Here's a way so you can get an idea:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Same page submit</title></head><body><?php if(isset($_POST['submit']) && !empty($_POST['fname']) && !empty($_POST['lname'])) // after submitting and all fields are filled, this gets ran and the form below disappears{  $firstName = $_POST['fname'];  $lastName = $_POST['lname'];  echo "<p>Hello $firstName $lastName, how are you today?</p>"; }else // when the page loads, this else clause gets ran first{  echo '<p style="color:red;">Please fill in all fields</p>';?><form action="samepageform.php" method="post"><label for="fname">First Name:</label><input type="text" name="fname" id="fname" /><label for="lname">Last Name:</label><input type="text" name="lname" id="lname" /><input type="submit" name="submit" /></form><?php}?></body></html>

Link to comment
Share on other sites

The page above is called samepageform.php. When clicking on submit, it goes to the same page. If you look at the comments, you can get an idea of whats going on. When the page first loads, since the 'if' is false, it will execute what's in the 'else' clause, which is the form itself. Once the user enters all the info and clicks on submit, since the page is submitting to itself, it goes through the whole if-else again, and this time the 'if' is executed(since it's true). not the 'else' clause thus not showing the form. You may be confused with how the else clause looks like. You can do that; you can mix HTML and PHP together like that. Here's basically what you're familiar with seeing. This example will always show the form no matter what:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Same page submit</title></head><body><?php if(isset($_POST['submit']) && !empty($_POST['fname']) && !empty($_POST['lname'])){  $firstName = $_POST['fname'];  $lastName = $_POST['lname'];  echo "<p>Hello $firstName $lastName, how are you today?</p>"; }else{  echo '<p style="color:red;">Please fill in all fields</p>';}?><form action="samepageform.php" method="post"><label for="fname">First Name:</label><input type="text" name="fname" id="fname" /><label for="lname">Last Name:</label><input type="text" name="lname" id="lname" /><input type="submit" name="submit" /></form></body></html>

Link to comment
Share on other sites

Also the php file samepageform... where is the code for that? I dont quite get how to use the code you offered. Please help.
samepageform.php IS your page and your action., hence why we keep telling you the form submits to itself. And it should be a PHP page, in case you weren't aware, but I am thinking you are trying to submit a form to another page. You need to realize both are the same in this situation.
have the form submit to itself. i.e. the action and the page with the form will be the same. Just make sure to check for submit in GET/POST so you don't get errors trying to echo out values when the haven't been set yet.
what i said is pretty much exampled by Don E'.s post. Like he said, the commenting should give you a good idea of how the logic is working.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...