I am learning php and mysqli (both of which are coming along ok) and I am trying to learn some Javascript too but I am getting completely lost with Javascript and I have no idea where I am going wrong. I have a form for changing an email address, the php validation works ok but I can't get any javascript to work. I would greatly appreciate it if someone could tell me where I am going wrong with this script. Thank you in advance.
<!DOCTYPE html>
<html lang="en">
<head>
<?php
ob_start();
session_start();
$timeout = 1800; // Number of seconds until it times out.
if (isset($_SESSION['user_id'])){
if(isset($_SESSION['timeout'])) {
$duration = time() - (int)$_SESSION['timeout'];
if($duration > $timeout) {
session_destroy();
}
}
}else{
Header("Location: login.php");
}
ob_end_flush();
// Update the timout field with the current time.
$_SESSION['timeout'] = time();
?>
<script type="text/javascript">
function validate(form){
fail = validateEmail(form.email.value)
if (fail == "") return true
else {alert(fail); return false}
}
</head>
<body>
<div class="content_two">
<?php
ob_start();
require_once ('mysqli_connect.php');
$errors = array();
$trimmed = array_map('trim', $_POST);
if (isset($_POST['submitted'])){
$em = FALSE;
if (preg_match ('/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$/',$trimmed['email'])){
$em = mysqli_real_escape_string ($dbc,$trimmed['email']);
}else{
$errors[] = 'Please enter a valid email address<br>';
}
if (empty($errors)) {
$q = "UPDATE users SET email = '$em' WHERE user_id={$_SESSION['user_id']} LIMIT 1";
$r = mysqli_query ($dbc, $q);
if (mysqli_affected_rows($dbc) == 1){
$url = 'myaccount.php';
ob_end_clean();
header("Location: $url");
}else{
echo 'update issue';
}
}else{
foreach ($errors as $msg){
echo " - $msg<br/>";
}
}
mysqli_close($dbc);
}
?>
<form action="my_account_c_email.php" method="post" onSubmit="return validate(this)">
<p><label for="email">* Email</label><input type="text" name="email" size="50" maxlength="50" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"/></p>
<p><input type="submit" name="submit" value="Update" id="button"/></p>
<input type="hidden" name="submitted" value="TRUE"/>
</form>
<script type="text/javascript">
function validateEmail(field) {
if (field == "") return "No email enetered".\
"
return""
}
</script>
</body>
</html>
replace top function like this
<script type="text/javascript">
function validateOld(form){
if (form.email.value != "") {
return true;
}else {
alert("No email enetered"); return false;
}
}
</script>
no need second script remove that "validateEmail" function
the best way to validate email is
function validate(form){
if (validateEmail(form.email.value)) {
return true;
}else {
alert("Email address is not valid!"); return false;
}
}
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
<form action="my_account_c_email.php" method="post" onSubmit="return validate(this)">
<p><label for="email">* Email</label><input type="text" name="email" size="50" maxlength="50" value=""/></p>
<p><input type="submit" name="submit" value="Update" id="button"/></p>
<input type="hidden" name="submitted" value="TRUE"/>
</form>
</div>
Syntax errors:
if (field == "") return "No email enetered".\
"
^--start string
^--end string
^^^^-- huh?
Since that's bad javascript, the entire script block gets killed, and the validate function is never defined.