Ajax and PHP

  • conn2prince
  • Graduate
  • Graduate
  • User avatar
  • Posts: 119

Post 3+ Months Ago

Hey guyz,

I got the following checkbox group:
Code: [ Select ]
<input id="chk[0]" name="chk[0]" value="Sam" onclick="fun_chk(this.value)" type="checkbox"/>Sam

<input id="chk[1]" name="chk[1]" value="Joe" onclick="fun_chk(this.value)" type="checkbox"/>Joe

<input id="chk[2]" name="chk[2]" value="Harrison" onclick="fun_chk(this.value)" type="checkbox"/>Harrison

<input id="chk[3]" name="chk[4]" value="Steve" onclick="fun_chk(this.value)" type="checkbox"/>Steve
  1. <input id="chk[0]" name="chk[0]" value="Sam" onclick="fun_chk(this.value)" type="checkbox"/>Sam
  2. <input id="chk[1]" name="chk[1]" value="Joe" onclick="fun_chk(this.value)" type="checkbox"/>Joe
  3. <input id="chk[2]" name="chk[2]" value="Harrison" onclick="fun_chk(this.value)" type="checkbox"/>Harrison
  4. <input id="chk[3]" name="chk[4]" value="Steve" onclick="fun_chk(this.value)" type="checkbox"/>Steve



When I select a check box, it should compare the variable set in the php file using AJAX. If the user checks the right one, the checkbox should remain as marked, else it needs to popup an alert box saying - 'Invalid Selection' and uncheck the box too.

Code: [ Select ]
$varName = 'Steve';



Plzzz help me out ! :cry:

Thx.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

If would be more efficient to include the correct answers on the page & check those rather than using AJAX.
  • conn2prince
  • Graduate
  • Graduate
  • User avatar
  • Posts: 119

Post 3+ Months Ago

html code :

Code: [ Select ]
<script>
function fun_chk(cname)
{
    var httpxml;
    try
    {
        // Firefox, Opera 8.0+, Safari
        httpxml=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            httpxml=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                httpxml=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    function stateck()
    {
        if(httpxml.readyState==4)
        {
            retVal = httpxml.responseText;
        }
    }
    var url="test.php";
    url=url+"?cname="+cname;
    httpxml.onreadystatechange=stateck;
    httpxml.open("GET",url,true);
    httpxml.send(null);
}
</script>

<input id="chk[0]" name="chk[0]" value="Sam" onclick="fun_chk(this.value)" type="checkbox"/>Sam

<input id="chk[1]" name="chk[1]" value="Joe" onclick="fun_chk(this.value)" type="checkbox"/>Joe

<input id="chk[2]" name="chk[2]" value="Harrison" onclick="fun_chk(this.value)" type="checkbox"/>Harrison

<input id="chk[3]" name="chk[4]" value="Steve" onclick="fun_chk(this.value)" type="checkbox"/>Steve
  1. <script>
  2. function fun_chk(cname)
  3. {
  4.     var httpxml;
  5.     try
  6.     {
  7.         // Firefox, Opera 8.0+, Safari
  8.         httpxml=new XMLHttpRequest();
  9.     }
  10.     catch (e)
  11.     {
  12.         // Internet Explorer
  13.         try
  14.         {
  15.             httpxml=new ActiveXObject("Msxml2.XMLHTTP");
  16.         }
  17.         catch (e)
  18.         {
  19.             try
  20.             {
  21.                 httpxml=new ActiveXObject("Microsoft.XMLHTTP");
  22.             }
  23.             catch (e)
  24.             {
  25.                 alert("Your browser does not support AJAX!");
  26.                 return false;
  27.             }
  28.         }
  29.     }
  30.     function stateck()
  31.     {
  32.         if(httpxml.readyState==4)
  33.         {
  34.             retVal = httpxml.responseText;
  35.         }
  36.     }
  37.     var url="test.php";
  38.     url=url+"?cname="+cname;
  39.     httpxml.onreadystatechange=stateck;
  40.     httpxml.open("GET",url,true);
  41.     httpxml.send(null);
  42. }
  43. </script>
  44. <input id="chk[0]" name="chk[0]" value="Sam" onclick="fun_chk(this.value)" type="checkbox"/>Sam
  45. <input id="chk[1]" name="chk[1]" value="Joe" onclick="fun_chk(this.value)" type="checkbox"/>Joe
  46. <input id="chk[2]" name="chk[2]" value="Harrison" onclick="fun_chk(this.value)" type="checkbox"/>Harrison
  47. <input id="chk[3]" name="chk[4]" value="Steve" onclick="fun_chk(this.value)" type="checkbox"/>Steve


PHP code :
Code: [ Select ]

$varName = 'Steve';

$cName = isset($_GET['cname']) ? $_GET['cname'] : '';

if($cName == $varName)
return 'Correct'
else
return 'Wrong';
  1. $varName = 'Steve';
  2. $cName = isset($_GET['cname']) ? $_GET['cname'] : '';
  3. if($cName == $varName)
  4. return 'Correct'
  5. else
  6. return 'Wrong';


If the user checks the right one, the checkbox should remain as marked, else it needs to popup an alert box saying - 'Invalid Selection' by getting the return value from PHP and uncheck the box too.

All these are missing now in my code :cry: :(
  • conn2prince
  • Graduate
  • Graduate
  • User avatar
  • Posts: 119

Post 3+ Months Ago

Is there anyway to make the above code work ?

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 158 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.