Retain Field Values After Submit
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
Solution 1:
Yes you could implement (some kind of) a flash session in this case:
So upon submission:
<?php
session_start(); // don't forgetrequire_once('../scripts/connect.php');
$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) ordie('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 itecho"
<script type='text/javascript'>
alert('The email address $email already exists');
</script>
";
}
?><formaction="register_script2.php"method="POST"name="form_register"id="form_registerID"accept-charset="UTF-8"><asideclass="field_reg_form"><inputname="field_email1"type="text"requiredid="field_email1ID"value="<?phpecho$email; ?>" /><br /><br /><inputname="field_email2"type="text"requiredid="field_email2ID"value="<?phpecho$email; ?>" /><br /><br /><inputtype="submit"value="submit"id="submit"name="submit" /></aside></form>
Sidenote: I suggest use prepared statements.
<?php
session_start(); // don't forgetrequire_once('../scripts/connect.php');
$con = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME) ordie('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";
}
?>
Solution 2:
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... --><formmethod="POST"><inputtype="email"name="email1"value="<?=$_POST['email1']; ?>" /><inputtype="email"name="email2"value="<?=$_POST['email2']; ?>" /><inputtype="submit"value /></form>
Then on the original request, output it:
<!-- ... --><?phpinclude('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):
<!-- ... --><?phpif (isset($_POST['submit'])) { ... } ?><!-- ... --><?phpinclude('register_form.php'); ?><!-- ... -->
Solution 3:
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';
}
...
Post a Comment for "Retain Field Values After Submit"