0

this question is really in two parts but they're relevant to each other _ i have created a contact form and want to give the form user a response message _ currently the message either appears at the top of the page or on a new page which forces me to tell the user to continue by pressing the back button _ i would like the message response to appear under the form AND (if it's possible!) allow the user to read the form response & continue browsing without having to do anything _ i am not very conversant with php but have gone through some answers i found on stack overflow and came up with this:

if (mail ($to, $subject, $body, $from)) { 
   echo '<div class="formOutput">'.'<p>Thank you for your email!<br />Please press your Back Button to continue</p>'.'</div>';
} else { 
   echo '<div class="formOutput">'.'<p>An error occurred. Please try sending your message again.<br />Please press your Back Button to continue</p>'.'</div>'; 
}

i then set div with class 'formOutput' underneath the form 'submit' button

 <input type="submit" value="Submit" name="submit">
 </form>
 <div class="formOutput" style="height: 50px;"></div>

unfortunately this has not worked _ the response messages are either appearing at the top of the page or on a new page _ depending on where i test the form _

as previously stated i would also like (if possible!) the form user to be able to read the response and then continue browsing without having to press 'back button' or anything else

thanks in advance for any assistance : )


i checked out another answer on stack overflow which covers part of the problem by setting the form action with a hash-tag which redirects to a specified element ID _ Contact Form Thank You Top of Page

however i'm not sure if it's possible or how this could be added to the action code i already have in my form =

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST"> 

answers relevant to the actual issue (rather than comments about the question formatting!) would be greatly appreciated _ thanks

Community
  • 1
  • 1
inputforcolor
  • 909
  • 2
  • 15
  • 27
  • Why the _? Keyboard broken? – George Kagan Oct 03 '16 at 11:35
  • There's lots of things wrong with this approach unfortunately... A user submitting a form then you asking the user to click "back" is very bad UX imo. You should submit the form using ajax and then use a success modal to say ok or not. – StudioTime Oct 03 '16 at 12:04
  • "very bad" _ i agree Darren! _ so if you're suggesting Ajax i have to assume there is no client-side solution to my issue! : ( thanks for your comment _ at least it gives me some direction : ) – inputforcolor Oct 03 '16 at 12:11

1 Answers1

2

looks like you redirect users to another page when they submit the form

<form name="my_form" method="post" action="page2.php" >

in "action" you can enter your current page destination and send the letter from this current page.

e.g. <form name="my_form" method="post" action="html_form.php" >

If you process the form in the same page, then you can display the message after the form is submitted. Its easier to do this if you give your submit button "name" value as well

e.g. <input type="submit" name="submit" value="Submit"><br/>

After the form is submitted you can write php code

<div>
<?php
if (isset($_POST["submit"])) {
echo "<p>Your message has been sent. Your message is {$_POST["message"]} </p>";}
?>
</div>

Basically you need to change "action " to the current page and process you $_POST variables in the same page