AJAX call will not work when using .change or .keydown

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

AJAX call will not work when using .change or .keydown

Post by pizzipie »

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: ======================== -->

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>
<!-- ==================== PHP ======================== -->

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>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: AJAX call will not work when using .change or .keydown

Post by Christopher »

Could be the URL is incorrect. Could be that the ajax request asks for "text" but the response is JSON.
(#10850)
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: AJAX call will not work when using .change or .keydown

Post by pizzipie »

Could be the URL is incorrect. Could be that the ajax request asks for "text" but the response is JSON.

URL is correct Ajax asks for text and PHP returns text.

$('#partName').keydown( function(e) {
if(e.which==13) {

name=$(this).val();
//name="George";

} // which
}); // keydown


This code is creating all the problems. If I feed the AJAX call something like var name='George' it all works fine.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: AJAX call will not work when using .change or .keydown

Post by Christopher »

Have to looked up what it should be rather than $(this).val(). I think it might be just this.value.
(#10850)
Post Reply