The $.ajax ({ ... Code Returns " Request failed: error" . Below is code that produced this. Firebug 'Net' doesn't show a call to the PHP file.
Note: If the keydown and related code is deleted and a button added to the html like:
<button type="button" id="send">Send me</button>
and execute like this:
$('#send').click(function() { var request=$.ajax ({ .... as below
Everything works fine.
Any help appreciated.
R
<!-- ================== HTML: ======================== -->
Code:
<form name="myName" id="myName" >
<p>To Search for a Name: Type Partial Name Then Enter: <br>
To Display all Names: Type All Then Enter:<br></p>
<p> <label for='partName'>Enter Name</label>
<input type="text" class='hov' name='partName' id='partName'/> </p>
</form>
<!-- ==================== PHP ======================== -->
PHP Code:
<?php
header('Content-Type: application/json'); // so JSON is
returned properlyset_include_path( '../include');
error_reporting(-1);ini_set("display_errors", true);
$name=$_POST['choice'];
echo "You have visited myTestAjax.php with data ...".$name." ... passed here.";
?>
<!-- =================== JQUERY/JAVASCRIPT ============= -->
Code:
<script type="text/javascript" >
$(document).ready(function() {
$('#partName').keydown( function(e) {
if(e.which==13) {
var name=$(this).val();
alert("keycode = "+e.keyCode);
var request = $.ajax({
url: "myTestAjax.php",
type: "POST",
data: { choice : name },
dataType: "text"
});
request.done(function( msg ) {
$( "#rbox" ).text( msg );
});
request.fail(function( jqXHR, textStatus ) {
document.write( "Request failed: " + textStatus );
});
} // if which
}); // partName
}); // ready
$('button[name="exit"]').click (function() { window.location.href='ExitProgram.php'});
</script>