Jump to content

PHP Echo when Form Is Submitted


kyleldi

Recommended Posts

Hi All,

 

So i'm attempting a script that basically echos "thank you for submitting" when a brief form is filled out.  I've been running into trouble because the information is posted to a sql db, and i could not get the script right so I resorted to dreamweaver for one of their scripts.  The only problem, is dreamweaver only allows the user to go to another page when the post is complete, where I'd like it to echo.  Here's my code, is it possible anyone can tell me what i need to modify?  I don't usually resort to scripts like this, but I need it to work and all...

 

Thanks everyone!

 

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "referral")) {
  $insertSQL = sprintf("INSERT INTO referring_website (referalid) VALUES (%s)",
                       GetSQLValueString($_POST['referral'], "int"));

  mysql_select_db($database_admin, $admin);
  $Result1 = mysql_query($insertSQL, $admin) or die(mysql_error());

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

 

Form Code:

 

        <form id="referral" name="referral" method="POST" action="<?php echo $editFormAction; ?>">
        <table width="76%" border="0">
          <tr>
            <td width="43%"><div align="right">ThomasNet</div></td>
            <td width="57%"> <div align="center">
              <input type="radio" name="referral" id="referral" value="2" />
            </div></td>
          </tr>
          <tr>
            <td><div align="right">Google</div></td>
            <td><div align="center">
              <input type="radio" name="referral" id="referral2" value="1" />
            </div></td>
          </tr>
          <tr>
            <td><div align="right">Directly</div></td>
            <td><div align="center">
              <input type="radio" name="referral" id="referral3" value="3" />
            </div></td>
          </tr>
        </table>
        <br />
        <input type="submit" name="submit" id="submit" value="Submit" />
        <input type="hidden" name="MM_insert" value="referral" />
        </form>

Link to comment
Share on other sites

One of the reason why they do that is so a person won't click refresh and keep inserting the same data over and over again. But if you want to echo out something without going to another page then change this

  $Result1 = mysql_query($insertSQL, $admin) or die(mysql_error());

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

 

To this

  $Result1 = mysql_query($insertSQL, $admin) or die(mysql_error());
  if(!$Result1){
  echo "could not insert data";
  } else {
  echo "Thank you for submitting your form";
  }
}

 

What I would do is echo out those results then use a meta tag to redirect the page to another location. You would have to use a meta tag because one you output the results to the browser you can no longer use the header function of php.

 

Ray

Link to comment
Share on other sites

<?php
$editFormAction = $_SERVER['PHP_SELF'];
if ($_GET['go'] == "post_data"){
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "referral")) {
  $insertSQL = sprintf("INSERT INTO referring_website (referalid) VALUES (%s)",
                       GetSQLValueString($_POST['referral'], "int"));

	 mysql_select_db($database_admin, $admin);
	 $Result1 = mysql_query($insertSQL, $admin) or die(mysql_error());

	 $insertGoTo = "index.php";
	 if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
	   $insertGoTo .= $_SERVER['QUERY_STRING'];
	 }
	 header("location:?go=complete");
}


}
if ($_GET['go'] == "complete"){
			print "<center><strong><p>thank you for submitting</p></strong></center>";
		}
?>

<form id="referral" name="referral" method="POST" action="<?php echo $editFormAction; ?>?go=complete">
        <table width="76%" border="0">
		<tr>
        		<td width="43%"><div align="right">ThomasNet</div></td>
            	<td width="57%"> <div align="center">
              	<input type="radio" name="referral" id="referral" value="2" />
            	</div></td>
          	</tr>
          	<tr>
            	<td><div align="right">Google</div></td>
            	<td><div align="center">
              	<input type="radio" name="referral" id="referral2" value="1" />
            	</div></td>
          	</tr>
          	<tr>
            	<td><div align="right">Directly</div></td>
            	<td><div align="center">
              	<input type="radio" name="referral" id="referral3" value="3" />
            	</div></td>
          	</tr>
        </table>
        <br />
        <input type="submit" name="submit" id="submit" value="Submit" />
        <input type="hidden" name="MM_insert" value="referral" />
</form>

 

From what I can see, this should work fine. <I hope>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.