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

Dani AI

Generated

Good catch from on using ==. Two other things made both buttons behave the same in your original snippet: you changed the form action via onclick (JavaScript can be skipped if the user hits Enter or JS is disabled), and both submit buttons shared the same name so isset($_POST['put']) could never tell them apart.

A simple, reliable pattern is to post to the same URL and branch on which button was clicked. No JS required, and it works with POST cleanly.

<form method="post" action="postAd.php">
  <input type="text" name="category">
  <input type="text" name="city">
  <input type="text" name="adTitle">
  <button type="submit" name="action" value="preview">Preview</button>
  <button type="submit" name="action" value="submit">Submit</button>
</form>
<?php
$action   = $_POST['action']   ?? '';
$category = trim($_POST['category'] ?? '');
$city     = trim($_POST['city']     ?? '');
$adTitle  = trim($_POST['adTitle']  ?? '');

if ($action === 'preview') {
    // Escape to avoid XSS in the preview
    echo '<h3>Preview</h3>';
    echo '<p>Category: ' . htmlspecialchars($category, ENT_QUOTES, 'UTF-8') . '</p>';
    echo '<p>City: ' . htmlspecialchars($city, ENT_QUOTES, 'UTF-8') . '</p>';
    echo '<p>Ad Title: ' . htmlspecialchars($adTitle, ENT_QUOTES, 'UTF-8') . '</p>';

    // Offer a confirm that posts the same data
    echo '<form method="post" action="postAd.php">';
    foreach (['category'=>$category,'city'=>$city,'adTitle'=>$adTitle] as $k=>$v) {
        echo '<input type="hidden" name="'.$k.'" value="'.htmlspecialchars($v, ENT_QUOTES, 'UTF-8').'">';
    }
    echo '<button type="submit" name="action" value="submit">Confirm and Post</button>';
    echo '</form>';
    exit;
}

if ($action === 'submit') {
    // Validate required fields
    if ($category === '' || $city === '' || $adTitle === '') {
        exit('Please fill in all fields.');
    }
    // Use PDO; mysql_* and magic_quotes are long gone
    $pdo = new PDO('mysql:host=localhost;dbname=class_ads;charset=utf8mb4', 'user', 'pass', [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
    $stmt = $pdo->prepare('INSERT INTO post_ad (cat_name, city_name, ad_title) VALUES (?, ?, ?)');
    $stmt->execute([$category, $city, $adTitle]);
    echo 'You successfully posted the advertisement.';
}

Notes:

  • Do not rely on onclick to decide server behavior.
  • Use strict comparison (===) and escape output in the preview.
  • Prefer table and column names without hyphens (e.g., post_ad) to avoid quoting headaches.

Recommended Answers

All 7 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.