Jump to content

Having problems with javascript code


tmharrison

Recommended Posts

I have my main website. www.mysite.com it has a tour number field on it, and it is working fine, however i would like to have javascript alerts when they either leave the field blank or the tour number doesn't exist in the database.  I have the alerts on my php page already.

 

anyway, i am ok with the way that it is, however, when i click the OK on the javascript alert message it continues to go onto my gotoTour.php script.  (which is listed below)

 

 

<?php
ob_start();
if (!empty($_POST['id_num'])) {

require_once ('/home/virttcom/public_html/VHDdb/mysql_connect.php');

// Clean the $_POST['id_num'] to avoid sql injection
if(get_magic_quotes_gpc()) {
	$_POST['id_num'] = stripslashes($_POST['id_num']);
}
$_POST['id_num'] = mysql_real_escape_string($_POST['id_num']);

$q = "SELECT url FROM Tours WHERE id_num='{$_POST['id_num']}'";

$get_url = mysql_query($q) or die("There was a problem with the query: $q" . mysql_error());

if (mysql_num_rows($get_url) > 0) {
	$rw = mysql_fetch_assoc($get_url);
	$url = $rw['url'];

	// Redirect the user
	header("location:$url");
} else {
		echo "<script language=\"javascript\"> alert('We do not carry this Virtual Tour, call your Agent and ask them to use Virtual Tours'); </script>"; 
}
} else {
		echo "<script language=\"javascript\"> alert('Virtual Tour Numbers are six numeric digits only. Please check your number and try again.'); </script>";
}
ob_end_flush();
?>

 

 

How can I get it to stop continuing to go to this php page, and just show the alert message on my main page www.mysite.com

 

Much appreciated, i've been messing with this all night now, i have tried everything been on every javascript site there is to figure it out, i am stuck.

 

Thanks again for your help

 

Tina

Link to comment
https://forums.phpfreaks.com/topic/37427-having-problems-with-javascript-code/
Share on other sites

Well one option is to check it before it is submitted using AJAX. If you check it after it has already been submitted, then do something like this:

echo "<script language=\"javascript\"> alert('We do not carry this Virtual Tour, call your Agent and ask them to use Virtual Tours'); 
history.go(-1); 
// or 
document.location.href = 'wherever.php';
</script>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.