i want to provide an option to user to preview the form input before form submission, it runs well, but it does the same action on pressing submission or preview button

html

<form>
<tr>
    <td width="26%" height="30" align="right"><font face="Calibri">Category:</font></td>
 	<input type="text" name="category" size="30"></td>
 
    <td width="26%" height="30" align="right"><font face="Calibri">City:</font></td>
 	<input type="text" name="city" size="30"></td>
    
	<td width="26%" height="30" align="right"><font face="Calibri">Ad Title:</font></td>
 	<input type="text" name="adTitle" size="30"></td>
 
 </tr>
</table>

<p align="center">
<input name="put" type="submit" value="Submit" onclick="action='postAd.php?action=submit';"> 
<input name="put" type="submit" value="Preview" onclick="action='postAd.php?action=preview';">
</p>
</form>

the php script is as follow

if(isset($_POST['put'])) {

	$category = $_POST['category'];
	$city     = $_POST['city'];
	$adTitle  = $_POST['adTitle'];

$action = $_POST['action'];
if ($action = 'preview')
{

echo $category;
echo $city;
echo $adTitle;
}

if ($action = 'submit')
{
if (!get_magic_quotes_gpc()) {

    $category = addslashes($category);
	$city     = addslashes($city);
	$adTitle  = addslashes($adTitle);
   }  
$sql = "INSERT INTO `class-ads`.`post-ad` (ad_id, cat_name, city_name, ad_title, )
		            VALUES (NULL,'$category', '$city', '$adTitle')";

mysql_query($sql) or die('Error, Posting Ad failed : ' . mysql_error());                    
echo "You successfully posted the advertisment";
}
}

when i press submit it enters all data to database, but the same thing happens after pressing preview button as well.
i have tried every option, i know but in vain..i am not understanding where i am doing wrong. please some one guide me in this regard.

thanks in advance

shuja

Use $action == 'preview' and $action=='submit' .
Also, mention Form's 'method'. If you don't mention it, I guess, the default method is GET.

If I may try my hand at this :D:

-Add the

method = "post"

to your form tag.

-Change

$action = $_POST['action'];

to

$action = $_GET['action'];

i am using POST method..
nav33n, when i use $action == 'preview' and $action=='submit', it does nothing with no error.

however, i just tried GET method, which is running fine.

But i wana use POST method....

According to your code, you are not using the POST method. To use that methhod' the code must read:

<form method="post">

Otherwise it defaults to get.

Cheers!

Via BlackBerry

Maybe this example will help you understand better.

<?php
if(isset($_POST['preview'])) {
	print "<pre>";
	print "Preview button pressed..<br>";
	print_r($_REQUEST);
	print "</pre>";
}
if(isset($_POST['submit'])) {
	print "<pre>";
	print "Submit button pressed..<br>";
	print_r($_REQUEST);
	print "</pre>";
}
?>
<form method='POST' action='postAd.php'>
<tr>
    <td width="26%" height="30" align="right"><font face="Calibri">Category:</font></td>
 	<input type="text" name="category" size="30"></td>
 
    <td width="26%" height="30" align="right"><font face="Calibri">City:</font></td>
 	<input type="text" name="city" size="30"></td>
    
	<td width="26%" height="30" align="right"><font face="Calibri">Ad Title:</font></td>
 	<input type="text" name="adTitle" size="30"></td>
 
 </tr>
</table>

<p align="center">
<input name="submit" type="submit" value="Submit""> 
<input name="preview" type="submit" value="Preview"">
</p>
</form>

I have named the buttons differently. I also don't need onclick events.

thanks for your valueable solutions...
please can you suggest me any tutorial for fruther and advance guidance in this regards...

shuja

You can start with w3schools for basics. w3schools cover most of the things you need to know. For more information, you can check php.net .

P.S. In my earlier example, there is an extra " for input type=submit.

This article has been dead for over six months. Start a new discussion instead.