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
Give brackets at end, so that it does understand that you are calling a function, also don't use confirm
as function name, its a native JS function, use my_confirm
instead.
onClick="my_confirm()"
Also $(this).val()
won't work in your case use $('#code').val()
instead.
$(document).ready(function() {
$('#confirm').click(function() {
$.post("",{code:$('#code').val() } ,function(data)
{
if(data=='sucess')
{
alert("yay");
}
else
{alert("nay");}
});
});
});
$(document).ready(function() {
$('#confirm').click(function() {
$.ajax({
url: "confirm.php",
type: "POST",
data: "code="+$('input[name="code"]').val(),
dataType: "text",
async:false,
success: function(msg){
if(msg=='sucess'){
alert('yay');
}else{
alert('nay');
}
}
});
});
});