-2

Does anyone know why this doesn't work? any help would be appreciated.

This is the HTML

Enter Code:<br/><input type="text" name="code" id="code"/><br/> 
<input type="button" id="confirm" value="Confirm" onClick="confirm()"/> 

This is the PHP (basically gets the value of the user input and if it's equal to the variable a echo sucess)

$code = $_POST["code"]; 
$a = 105678; 
if($code==$a){ 
  echo "sucess"; 
} else { 
  echo "nosucces"; 
} 

The JavaScript (simple ajax code to alert yay on PHP sucess or nay on no sucess)

function confirm () { 
  $.post(
    "confirm.php",
    {code:$(this).val() },
    function(data) { 
      if(data=='sucess') { 
        alert("yay"); 
      } else { 
        alert("nay");
      } 
    }
  ); 
}

basically on all occasions the ouput is nay on further debugging i tried an elseif statement on the javascript asking if data is equal to nosucces but it didn't even alert anything, meaning it's not getting the data from the php

7
  • 3
    Please when asking a programming question, never describe the problem as "doesn't work". Please describe exactly what goes wrong how, and what errors you get in the browser console (if any). Thanks! Commented Sep 23, 2011 at 9:22
  • 1
    What exactly "doesn't work"? What do you expect to happen and what happens instead? Commented Sep 23, 2011 at 9:22
  • what you are geting now? Commented Sep 23, 2011 at 9:23
  • @Tommy: Pro tip: Put more effort into formatting your code. Commented Sep 23, 2011 at 9:28
  • basically on all occasions the ouput is nay on further debugging i tried an elseif statement on the javascript asking if data is equal to nosucces but it didn't even alert anything, meaning it's not getting the data from the php.. Commented Sep 23, 2011 at 9:36

1 Answer 1

2
    $(document).ready(function() {
    $('#confirm').click(function() {
        $.post("",{code:$('#code').val() } ,function(data)
        {
            if(data=='sucess')
            {
                alert("yay");
            }
            else
            {alert("nay");}
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments