Jump to content
confused and dazed

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

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?

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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>

Edited by Don E

Share this post


Link to post
Share on other sites

Don E: the curly brackets look strange in your code. Also the php file samepageform... where is the code for that? I dont quite get how to use the code you offered. Please help. Justsomeguy: I will look through the information you sent and look for the parts that would apply to my question. Thanks.

Share this post


Link to post
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>

Edited by Don E

Share this post


Link to post
Share on other sites

I used the code you typed in verbatim and when the page loads (after submit button is pressed) there is an error message that states website cannot be found. Do I need to name my .html a certain name?Not sure how to fix that.

Edited by confused and dazed

Share this post


Link to post
Share on other sites

You need to name it samepageform.php, or change the action in the form to whatever you named the file. This is a PHP file, by the way, not a .html file.

Share this post


Link to post
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. Edited by thescientist

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...