preview option before form submission

Reply

Join Date: May 2008
Posts: 74
Reputation: servis is an unknown quantity at this point 
Solved Threads: 0
servis servis is offline Offline
Junior Poster in Training

preview option before form submission

 
0
  #1
Mar 2nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,748
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: preview option before form submission

 
0
  #2
Mar 2nd, 2009
Use $action == 'preview' and $action=='submit' .
Also, mention Form's 'method'. If you don't mention it, I guess, the default method is GET.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Re: preview option before form submission

 
0
  #3
Mar 2nd, 2009
If I may try my hand at this :

-Add the
  1. method = "post"
to your form tag.

-Change
  1. $action = $_POST['action'];
to
  1. $action = $_GET['action'];
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 74
Reputation: servis is an unknown quantity at this point 
Solved Threads: 0
servis servis is offline Offline
Junior Poster in Training

Re: preview option before form submission

 
0
  #4
Mar 2nd, 2009
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....
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 64
Reputation: jeffc418 is an unknown quantity at this point 
Solved Threads: 0
jeffc418 jeffc418 is offline Offline
Junior Poster in Training

Re: preview option before form submission

 
0
  #5
Mar 2nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,748
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: preview option before form submission

 
0
  #6
Mar 3rd, 2009
Maybe this example will help you understand better.
  1. <?php
  2. if(isset($_POST['preview'])) {
  3. print "<pre>";
  4. print "Preview button pressed..<br>";
  5. print_r($_REQUEST);
  6. print "</pre>";
  7. }
  8. if(isset($_POST['submit'])) {
  9. print "<pre>";
  10. print "Submit button pressed..<br>";
  11. print_r($_REQUEST);
  12. print "</pre>";
  13. }
  14. ?>
  15. <form method='POST' action='postAd.php'>
  16. <tr>
  17. <td width="26%" height="30" align="right"><font face="Calibri">Category:</font></td>
  18. <input type="text" name="category" size="30"></td>
  19.  
  20. <td width="26%" height="30" align="right"><font face="Calibri">City:</font></td>
  21. <input type="text" name="city" size="30"></td>
  22.  
  23. <td width="26%" height="30" align="right"><font face="Calibri">Ad Title:</font></td>
  24. <input type="text" name="adTitle" size="30"></td>
  25.  
  26. </tr>
  27. </table>
  28.  
  29. <p align="center">
  30. <input name="submit" type="submit" value="Submit"">
  31. <input name="preview" type="submit" value="Preview"">
  32. </p>
  33. </form>
I have named the buttons differently. I also don't need onclick events.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 74
Reputation: servis is an unknown quantity at this point 
Solved Threads: 0
servis servis is offline Offline
Junior Poster in Training

Re: preview option before form submission

 
0
  #7
Mar 3rd, 2009
thanks for your valueable solutions...
please can you suggest me any tutorial for fruther and advance guidance in this regards...

shuja
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,748
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: preview option before form submission

 
0
  #8
Mar 3rd, 2009
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.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC