Page 1 of 1

How to stop form from generating new page

Posted: Mon Dec 14, 2009 9:29 am
by verlager
This is xHTML / php code to grab a few fields and write them to a "|" delimited file for later import into an Excel/OO spreadsheet.

Code: Select all

 
<div id="stylized" class="myform">
<form id="form" action="mailer.php?savedata=1" method="post">
<fieldset>
<h1>Tell us about yourself!</h1>
 
<label>Name</label><input type="text" name="name" id="name" maxlength="25" />
<label>Email</label><input type="text" name="email" id="email" maxlength="25" />
<label>Country</label><input type="text" name="country" id="country" />
<label>Rating</label><input type="text" name="rating" id="rating" size="5" />
<br class="clear" />
 
List chess goals and other pertinent information:<br /> 
<textarea name="message" rows="5" cols="60"></textarea><br />
 
<input type="submit" name="submit" value="Submit" />
<input type="reset" value="Reset" name="reset" />
</fieldset>
</form>
</div>
 
The problem is that the mailer.php below generates a new page and messes things up. I would like to place an <alert> to inform the user that his message has been sent. I would also like to clear the form (already done) and stay or reload the original page (code listed above). Any suggestions, ideas would be welcome.

Code: Select all

 
<?php
$savedata = $_REQUEST['savedata'];
if ($savedata == 1){ 
$data = date("ymdHis");
$data .= "|";
$data .= $_POST['name'];
$data .= "|";
$data .= $_POST['email'];
$data .= "|";
$data .= $_POST['country'];
$data .= "|";
$data .= $_POST['rating'];
$data .= "|";
$data .= $_POST['message'];
$data .= "\n";
$file = "./datafile.txt"; 
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!"); 
fclose($fp); 
}
?>
 

Re: How to stop form from generating new page

Posted: Mon Dec 14, 2009 9:59 am
by AbraCadaver
Whenever you post a form to another page, the browser will be directed to that page. You can redirect them back:

Code: Select all

header("Location: http://www.example.com/original_form_page.html");
exit;

Re: How to stop form from generating new page

Posted: Mon Dec 14, 2009 9:54 pm
by verlager
Wow! Thank you very much!

All I needed was to add your code and a small snippet with the 'alert' function:

<div class="center" style="text-align:center; width:25em; margin: 0 auto">
<input type="submit" name="submit" value="Submit" onclick="alert('Your message has been sent!')" />
<input type="reset" value="Reset" name="reset" />
</div>

Re: How to stop form from generating new page

Posted: Mon Dec 14, 2009 10:52 pm
by Griven
Or you could get fancy with some Jquery and AJAX: http://net.tutsplus.com/javascript-ajax ... ng-jquery/

Re: How to stop form from generating new page

Posted: Mon Dec 14, 2009 10:57 pm
by verlager
Yes. Good idea, and I use jQuery. But why add complexity to a working solution that has just two lines of php code?

Re: How to stop form from generating new page

Posted: Tue Dec 15, 2009 1:42 am
by Griven
If it works and you're happy with it, then there's no reason to add such complexity. If you want something to give the user a "rich application" experience, then such complexity is well worth it. It all depends on what your goals are.