Addform.php go to url on success
2 posts by 2 authors in: Forums > CMS Builder
Last Post: April 27, 2016 (RSS)
By JeffC - April 26, 2016
Sorry, I am asking a lot of questions today.
I would like a user to be redirected to a url after successfully completing a form. Below is addform.php, available from this forum.
I would like to add something like <form method="POST" action="https://secure.nochex.com/"> so that when submitting the form the user is taken to a payment page, the problem is by doing this all of the error checking is ignored and it is possible to submit a blank form.
Any help gratefully received.
Thanks
<?php
require_once "../lib/init.php";
// submit form
if (@$_REQUEST['submit']) {
// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['title']) { $errorsAndAlerts .= "Please specify title!<br/>\n"; }
if (!@$_REQUEST['content']) { $errorsAndAlerts .= "Please specify content!<br/>\n"; }
// turn off strict mysql error checking for: STRICT_ALL_TABLES
mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
// add record
if (!@$errorsAndAlerts) {
mysql_query("INSERT INTO `{$TABLE_PREFIX}news` SET
title = '".mysql_real_escape_string( $_REQUEST['title'] )."',
content = '".mysql_real_escape_string( $_REQUEST['content'] )."',
createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();
// display thanks message and clear form
$errorsAndAlerts = "Thanks, we've added that record!";
$_REQUEST = array();
}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form method="post" action="">
<input type="hidden" name="submit" value="1" />
<h1>Sample Record Add Form</h1>
<?php if (@$errorsAndAlerts): ?>
<div style="color: red; font-weight: bold; font-size: 16px;"><br/>
<?php echo $errorsAndAlerts; ?><br/><br/>
</div>
<?php endif ?>
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<td valign="top">Title</td>
<td><input type="text" name="title" value="<?php echo htmlspecialchars(@$_REQUEST['title']) ?>" size="30" /></td>
</tr>
<tr>
<td valign="top">Content</td>
<td><textarea name="content" cols="30" rows="4"><?php echo htmlspecialchars(@$_REQUEST['content']) ?></textarea></td>
</tr>
</table><br/><br/>
<input type="submit" name="add" value="Add Record >>" />
</form>
</body>
</html>
By Damon - April 27, 2016
Hi Jeff,
Try adding the code in bold. After the error checking is complete and the record is added to the database, the user will be redirected to the page you set.
<?php
require_once "../lib/init.php";
// submit form
if (@$_REQUEST['submit']) {
// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['title']) { $errorsAndAlerts .= "Please specify title!<br/>\n"; }
if (!@$_REQUEST['content']) { $errorsAndAlerts .= "Please specify content!<br/>\n"; }
// turn off strict mysql error checking for: STRICT_ALL_TABLES
mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
// add record
if (!@$errorsAndAlerts) {
mysql_query("INSERT INTO `{$TABLE_PREFIX}news` SET
title = '".mysql_real_escape_string( $_REQUEST['title'] )."',
content = '".mysql_real_escape_string( $_REQUEST['content'] )."',
createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();
// display thanks message and clear form
//$errorsAndAlerts = "Thanks, we've added that record!";
//$_REQUEST = array();
header("Location: https://secure.nochex.com/");
}
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form method="post" action="">
<input type="hidden" name="submit" value="1" />
<h1>Sample Record Add Form</h1>
<?php if (@$errorsAndAlerts): ?>
<div style="color: red; font-weight: bold; font-size: 16px;"><br/>
<?php echo $errorsAndAlerts; ?><br/><br/>
</div>
<?php endif ?>
<table border="0" cellspacing="0" cellpadding="2">
<tr>
<td valign="top">Title</td>
<td><input type="text" name="title" value="<?php echo htmlspecialchars(@$_REQUEST['title']) ?>" size="30" /></td>
</tr>
<tr>
<td valign="top">Content</td>
<td><textarea name="content" cols="30" rows="4"><?php echo htmlspecialchars(@$_REQUEST['content']) ?></textarea></td>
</tr>
</table><br/><br/>
<input type="submit" name="add" value="Add Record >>" />
</form>
</body>
</html>
Damon Edis - interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/