I need the action of this form to call my script, then if the email address is already in the database it should show the alert.
This works but obviously I'm directed to my blank script page and when I return to the form, the data is gone.
I would like to be redirected to the form with the data intact and show the alert.
I've tried all day long to get sessions to work and now I'm just confused.
If someone could show me what and where to add the session code for each page, I would really appreciate it.
This page holds 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 included form page:
<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";
}
}
?>
Yes you could implement (some kind of) a flash session in this case:
So upon submission:
<?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 in the form page add this as well:
<?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 use prepared statements.
<?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";
}
?>
You can use ajax
to send data and then showing the return of your php
script, like this :
$.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 your PHP
code do this ;
...
if(mysqli_num_rows($sql) > 0 ){
echo $var_Email1;
} else {
echo 'error';
}
...
I know you posted code, but I'm still; not sure on workflow. However, I'll give you a "for instance" and see if you can at least run with the idea.
Given your form, also provide the value
attributes and pull them in from the $_POST
values:
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 on the original request, output it:
<!-- ... -->
<?php include('register_form.php'); ?>
<!-- ... -->
Then, within the page you're submitting to also call it (this time it'll be pre-populated by the incoming values):
<!-- ... -->
<?php if (isset($_POST['submit'])) { ... } ?>
<!-- ... -->
<?php include('register_form.php'); ?>
<!-- ... -->