LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2014, 03:10 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
AJAX call will not work when using .change or .keydown


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>
 
Old 09-05-2014, 12:06 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
The second line of the PHP code is clearly wrong. It looks like you improperly broke the line comment above it.
 
Old 09-05-2014, 01:15 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks dugan. I checked the actual code I run and it is ok. I've tracked the problem to:

$('#partName').keydown( function(e) {
if(e.which==13) {
name=$(this).val();
//name="george";
} // which
}); // keydown


if a 'name' is assigned to name and passed to AJAX it all works fine. Just can't get the 'keydown' code to work!!.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP - in-data from AJAX call will not work in SYSTEM() - using bash same data works pizzipie Programming 6 06-28-2014 12:30 AM
Can't display data returned from AJAX call pizzipie Programming 2 06-17-2014 01:37 PM
[SOLVED] Exposing a feebnack value from withing an AJAX call-back function... ButterflyMelissa Programming 4 11-24-2013 03:16 PM
How to change the background of a webpage in Ajax? resetreset Programming 1 01-22-2011 04:38 AM
Firefox Specific Refresh Problem. Ffox Can't remember redirected URL after ajax call supertramp Programming 2 07-20-2009 07:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration