Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a PHP script using makeRequest

Status
Not open for further replies.

kalekseishaken

Programmer
Joined
Mar 7, 2006
Messages
6
Location
US
I have a problem that I am hoping someone can help me with.

First let me describe the problem. I have an HTML form that in one field has an onBlur call to a JavaScript function. When you exit the field, it calls the JavaScript routine, which in turn calls a PHP script. The PHP script runs returns the value needed. And returns to the JavaScript. The JavaScript function is then supposed to set alerts depending on the values returned from the PHP script. It doesn't. If you then move the cursor into the field and exit again, it again runs the JavaScript which calls the PHP script then proceeds to show the alerts exactly like it is supposed to.

I did some experimenting, and if you use a button to run the JavaScript, the exact same thing happens. Click the button and the PHP script sends back the value, but no alert. You click the button a second time, and the alert shows.

I am using the JavaScript monitor in FireFox v1.5 so I can see that the PHP script is being called and returning the correct value the first time. So what I am trying to figure out is why the routine has to be called twice before I get my alert.

Below is the HTML, JavaScript & PHP that I am using.

Thanks,
C.Joseph


>>>>>>>>>>> Start HTML/JavaScript Page <<<<<<<<<<<<<

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"<html>
<head>
<title>Test New Member Creation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
font-size: 36px;
font-weight: bold;
}
body {
background-color: #FEFED6;
}
.style2 {font-size: x-large}
-->
</style>
</head>

<body>

<p align="center" class="style1">Start Membership</p>

<script language="javascript" type="text/javascript">

var http_request = false ;
var ctyfnd

function chkcity()
{
tmp1 = document.crnew.stt.value ;
tmp2 = document.crnew.cty.value ;
prm="?stt="+tmp1+"&cty="+tmp2;
makeRequest('chk2data.php',prm) ;
thrspns = result.indexOf("fou~~und");
if (thrspns<0) {
ctyfnd="no" ;
} else {
ctyfnd="yes";
}
reqcity() ;
}

function reqcity()
{
if (ctyfnd == "no")
{
alert("We can not find a City named "+tmp2) ;
document.crnew.cty.focus() ; }
}


function makeRequest(url, parameters)
{
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('GET', url + parameters, true);
http_request.send(null);
}


function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
} else {
alert('There was a problem with the request.');
}
}
}
</script>

<form method="" name="crnew" action="">
<table width="400" border="1" align="center">
<tr>
<td>State</td>
<td> <select name="stt" size="1">
<option value="0">- - Please Select - -</option>
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option>
<option value="5">California</option>
<option value="6">Colorado</option>
<option value="7">Connecticut</option>
<option value="8">Delaware</option>
<option value="9">Florida</option>
<option value="10">Georgia</option>
<option value="11">Hawaii</option>
<option value="12">Idaho</option>
<option value="13">Illinois</option>
<option value="14">Indiana</option>
<option value="15">Iowa</option>
<option value="16">Kansas</option>
<option value="17">Kentucky</option>
<option value="18">Louisiana</option>
<option value="19">Maine</option>
<option value="20">Maryland</option>
<option value="21">Massachusetts</option>
<option value="22">Michigan</option>
<option value="23">Minnesota</option>
<option value="24">Mississippi</option>
<option value="25">Missouri</option>
<option value="26">Montana</option>
<option value="27">Nebraska</option>
<option value="28">Nevada</option>
<option value="29">New Hampshire</option>
<option value="30">New Jersey</option>
<option value="31">New Mexico</option>
<option value="32">New York</option>
<option value="33">North Carolina</option>
<option value="34">North Dakota</option>
<option value="35">Ohio</option>
<option value="36">Oklahoma</option>
<option value="37">Oregon</option>
<option value="38">Pennsylvania</option>
<option value="39">Rhode Island</option>
<option value="40">South Carolina</option>
<option value="41">South Dakota</option>
<option value="42">Tennessee</option>
<option value="43">Texas</option>
<option value="44">Utah</option>
<option value="45">Vermont</option>
<option value="46">Virginia</option>
<option value="47">Washington</option>
<option value="48">West Virginia</option>
<option value="49">Wisconsin</option>
<option value="50">Wyoming</option>
<option value="51">District of Columbia</option>
</select></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="cty" onBlur="chkcity();"/></td>
</tr>
<tr>
<td>Zip Code </td>
<td><input type="text" name="zpcd"/></td>
</tr>
</table>
<input type="button" name="button" value="Pay for Membership" onclick="VerifyEntries()"/>
</p>
</form>
</body>
</html>

>>>>>>>>>>> End HTML/JavaScript Page <<<<<<<<<<<<<

>>>>>>>>>>> Start PHP Script <<<<<<<<<<<<<
<?php
$host = "my.server.net";
$login = "loginname";
$pwd = "password";
$port = "";
$db = "database";
$fln = "table" ;
$prma = $_REQUEST["stt"] ;
$prmb = $_REQUEST["cty"] ;
$host=$host.":".(integer)$port;
$conn=mysql_connect($host,$login,$pwd) or die();
$sql = "SELECT * FROM $fln WHERE stt='$prma' AND cty='$prmb'";
mysql_select_db($db,$conn) ;
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if ($num_rows==1) { echo "fou~~und" ; }
else{ echo "dec~~ine" ; }
?>
>>>>>>>>>>> End PHP Script <<<<<<<<<<<<<
 
The JavaScript function is then supposed to set alerts depending on the values returned from the PHP script. It doesn't.

Click the button and the PHP script sends back the value, but no alert.

Perhaps this is because you are never alerting the returned value? The only alerts I can see are error messages - there are no alerts that show any returned data.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi BillyRay,

The problem is I only want an error alert if the PHP script returns "dec~~ine" if the if the city is found then I just want to move to the next field.

As near as I can tell. It is a programming problem. The PHP script has not returned a result before the JavaScript is testing for its value.

If I test first to see if a value has been returned, then I get the error that there has been to many recursions waiting for the response.

Ciao . . . C.Joseph
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top