Retain field values โโafter submission
I need an action on this form to call my script, and then if the email is already in the database, it should show a warning.
This works, but obviously I am redirected to my blank script page and when I return to the form the data is gone.
I would like to redirect to a form with intact data and show a warning.
I've been trying all day to get sessions to work and now I'm just confused.
If someone can show me what and where to add session code for each page, I would really appreciate it.
This page contains my form:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script>
<script src='http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js'></script>
<script src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js'></script>
<script src="../js/register_validate.js" type="text/javascript"></script>
<link href="../css/styles.css" rel="stylesheet" type="text/css">
<base target="_top">
</head>
<body>
<p><?php include ('register_form2.php') ?></p>
</body>
</html>
This is the page with the included form:
<form action="register_script2.php" method="POST" name="form_register" id="form_registerID" accept-charset="UTF-8">
<aside class="field_reg_form">
<input name="field_email1" type="text" required id="field_email1ID" /><br /><br />
<input name="field_email2" type="text" required id="field_email2ID" /><br /><br />
<input type="submit" value="submit" id="submit" name="submit" />
</aside>
</form>
Here is the PHP script:
<?php
require_once('../scripts/connect.php');
$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');
if(isset($_POST['submit'])) {
$var_Email1 = mysqli_real_escape_string($con, $_POST['field_email1']);
$var_Email2 = mysqli_real_escape_string($con, $_POST['field_email2']);
if ($var_Email1 == $var_Email2){
$sql = mysqli_query($con, "SELECT * FROM membership WHERE Email = '$var_Email1' ");
if(mysqli_num_rows($sql) > 0 ){
print '<script type="text/javascript">';
print 'alert("The email address '. $_POST['field_email1'].' is already in our database")';
print '</script>';
exit();
}
echo "not in database";
}
}
?>
source to share
Yes, you could implement (some sort of) a flash session in this case:
So, when serving:
<?php
session_start(); // don't forget
require_once('../scripts/connect.php');
$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');
if(isset($_POST['submit'])) {
$var_Email1 = mysqli_real_escape_string($con, $_POST['field_email1']);
$var_Email2 = mysqli_real_escape_string($con, $_POST['field_email2']);
if ($var_Email1 == $var_Email2){
$sql = mysqli_query($con, "SELECT * FROM membership WHERE Email = '$var_Email1' ");
if(mysqli_num_rows($sql) > 0){
// set session
$_SESSION['email_exists'] = $var_Email1;
header('Location: the_starting_php.php');
exit;
}
}
echo "not in database";
}
?>
And then on the form page add this also:
<?php
session_start();
// check if there is
$email = '';
if(isset($_SESSION['email_exists'])) {
$email = $_SESSION['email_exists'];
unset($_SESSION['email_exists']); // unset it
echo "
<script type='text/javascript'>
alert('The email address $email already exists');
</script>
";
}
?>
<form action="register_script2.php" method="POST" name="form_register" id="form_registerID" accept-charset="UTF-8">
<aside class="field_reg_form">
<input name="field_email1" type="text" required id="field_email1ID" value="<?php echo $email; ?>" />
<br /><br />
<input name="field_email2" type="text" required id="field_email2ID" value="<?php echo $email; ?>" />
<br /><br />
<input type="submit" value="submit" id="submit" name="submit" />
</aside>
</form>
Sidenote: I suggest using prepared instructions.
<?php
session_start(); // don't forget
require_once('../scripts/connect.php');
$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) or die('Could not connect to database server.');
if(isset($_POST['submit'])) {
$var_Email1 = $_POST['field_email1'];
$var_Email2 = $_POST['field_email2'];
if ($var_Email1 == $var_Email2){
$sql = 'SELECT * FROM membership WHERE Email = ?';
$select = $con->prepare($sql);
$select->bind_param('s', $var_Email1);
$select->execute();
if($select->num_rows > 0){
// set session
$_SESSION['email_exists'] = $var_Email1;
header('Location: the_starting_php.php');
exit;
}
}
echo "not in database";
}
?>
source to share
You can use ajax
to send data and then show the return of your php
script, like:
$.ajax({
type: 'POST',
url: 'register_script2.php',
data: $('form#form_register').serialize()
})
.done(function(msg) {
if(msg != 'error'){
alert('The email address '+msg+' is already in our database');
} else {
// init your form
}
});
In code, php
do this;
...
if(mysqli_num_rows($sql) > 0 ){
echo $var_Email1;
} else {
echo 'error';
}
...
source to share
I know you posted the code, but I am still; not sure about the workflow. However, I'll give you an example and see if you can even get the idea started.
Given your shape, also specify the attributes value
and pull them out of the values $_POST
:
register_form.php
<!-- keeping it brief... -->
<form method="POST">
<input type="email" name="email1" value="<?= $_POST['email1']; ?>" />
<input type="email" name="email2" value="<?= $_POST['email2']; ?>" />
<input type="submit" value />
</form>
Then, in your original query, output it:
<!-- ... -->
<?php include('register_form.php'); ?>
<!-- ... -->
Then, on the page you are submitting is also called (this time it will be filled with incoming values):
<!-- ... -->
<?php if (isset($_POST['submit'])) { ... } ?>
<!-- ... -->
<?php include('register_form.php'); ?>
<!-- ... -->
source to share