AJAX call will not work when using .change or .keydown
Posted: Thu Sep 04, 2014 3:01 pm
The $.ajax ({ ... Code Returns " Request failed: error" . Below is code that produced this. The Firebug console is blank and 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 Name</button> and executed like this: $('#send').click(function() { var request=$.ajax ({ .... as below
Everything works fine.
Any help appreciated.
R
<!-- ================== HTML: ======================== -->
<!-- ==================== PHP ======================== -->
<!-- =================== JQUERY/JAVASCRIPT ============= -->
Note: If the keydown and related code is deleted and a button added to the html like: <button type="button" id="send">Send Name</button> and executed like this: $('#send').click(function() { var request=$.ajax ({ .... as below
Everything works fine.
Any help appreciated.
R
<!-- ================== HTML: ======================== -->
Code: Select all
<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>
Code: Select all
<?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: Select all
<script type="text/javascript" >
$(document).ready(function() {
$('#partName').keydown( function(e) { [color=#00BFFF] // If you used $('#'partName').change( function() { ... ajax call[/color] doesn't work either.
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>