PDA

View Full Version : From php to php using ajax


phingko
04-07-2010, 12:29 PM
Hi people,
I don't know how to put it in proper words for this question.
Hope you get what I mean in the end of this post.

I am using ajax to show a member details when member click log in (details was echo in php file),after he log in, he can book the ticket (the button is in the php file) and when it was clicked,it will validate and go to AJAX to show the booking details.
What I want to know is, is it possible for the AJAX to work if the button inside a php file which have button that has the AJAX function. Because I tried it, and it didn't even go to my js file.

To get you understand more,please take a look at my code below;

in login.php:

while($row = mysql_fetch_array($result))
{
if($name == $row['member'] && $pwd == $row['pass'])
{
if ($row['category'] == "adult")
{
print "<table border='1'>
<tr>
<td align='right'>Book Ticket</td>
<td align='left'><input type='radio' name='concession' value='1' checked/>Concession Ticket</td>
<td>$150</td>
</tr>
<tr>
<td align='left' colspan=3> </td>
</tr>
<tr>
<td align='right' colspan='3'><input name='submit' type = 'button' value = 'Book' onClick = 'displayCon(\"book.php\", \"targetNew\",concession.value);'></td>
</tr>
</table>";
}
}
}

while the js file was linked from the html page

this is the ajax;

function displayCon(dataSource, divID, aCon) {
alert(aCon);
var flag= validate();
if(flag)
{
var xhr = createRequest();
if(xhr) {
var obj = document.getElementById(divID);
//var obj = document.simple.member.value;
var requestbody ="Con="+encodeURIComponent(aCon);
xhr.open("POST", dataSource, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
obj.innerHTML =xhr.responseText;
}
}
}
xhr.send(requestbody);
}
}

As you can see, I put the alert, but it didn't work at all.

Please help me.
Point out what I have been doing wrong.

Thank You in advanced

mic2100
04-07-2010, 03:28 PM
hi,

try this:

function displayCon(dataSource, divID) {
var aCon;
if(document.getElementById("concession").checked === true){aCon = 1;}
else{aCon = 0;}
alert(aCon);

var flag= validate();
if(flag)
{
var xhr = createRequest();
if(xhr) {
var obj = document.getElementById(divID);
//var obj = document.simple.member.value;
var requestbody ="Con="+encodeURIComponent(aCon);
xhr.open("POST", dataSource, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
obj.innerHTML =xhr.responseText;
}
}
}
xhr.send(requestbody);
}
}


and:

while($row = mysql_fetch_array($result))
{
if($name == $row['member'] && $pwd == $row['pass'])
{
if ($row['category'] == "adult")
{
print "<table border='1'>
<tr>
<td align='right'>Book Ticket</td>
<td align='left'><input type='radio' name='concession' value='1' checked/>Concession Ticket</td>
<td>$150</td>
</tr>
<tr>
<td align='left' colspan=3> </td>
</tr>
<tr>
<td align='right' colspan='3'><input name='submit' type = 'button' value = 'Book' onClick = 'displayCon(\"book.php\", \"targetNew\");'></td>
</tr>
</table>";
}
}
}


the problem was that you was trying to access the form element "concession.value" from within the onclick event, i would advise using JS to check these values once in the JS function.