2017-02-22 4 views
1

У меня есть код ниже. В настоящее время он представляет только первое имя, которое именно то, что я хочу, и это делает «на лету» без обновления страницы.Идентификатор сообщения на клавиатуре AJAX/PHP/MYSQL

Однако мне нужно проанализировать идентификационный номер по одному и тому же запросу ajax через скрытое поле id.

Можно ли предложить какие-либо идеи, чтобы помочь мне?

Заранее спасибо

КОД:

<html> 
<head> 
<script> 
function ajax_post(){ 
    // Create our XMLHttpRequest object 
    var hr = new XMLHttpRequest(); 
    // Create some variables we need to send to our PHP file 
    var url = "my_parse_file.php"; 
     var fn = document.getElementById("first_name").value; 
    var vars = "firstname="+fn+; 
    hr.open("POST", url, true); 
    // Set content type header information for sending url encoded variables  in the request 
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    // Access the onreadystatechange event for the XMLHttpRequest object 
    hr.onreadystatechange = function() { 
     if(hr.readyState == 4 && hr.status == 200) { 
      var return_data = hr.responseText; 
      document.getElementById("status").innerHTML = return_data; 
     } 
    } 
    // Send the data to PHP now... and wait for response to update the status div 
    hr.send(vars); // Actually execute the request 
    document.getElementById("status").innerHTML = "processing..."; 
} 
</script> 
</head> 
<body> 
<h2>Ajax Post to PHP and Get Return Data</h2> 
<input type="hidden" name="my_id" value="1234"> 
First Name: <input id="first_name" name="first_name" type="text" onkeyup="ajax_post();"> <br><br> 
<div id="status"></div> 
</body> 
</html> 
+1

вар вары = "Имя =" + п + '& my_id =' + документ. . getElementById ("my_id") значение; Surace

ответ

0

Использование JQuery

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>TITLE</title> 
 
\t <script src="https://code.jquery.com/jquery-3.1.1.min.js" type="text/javascript"></script> 
 
\t <script type="text/javascript"> 
 
function ajax_post(){ 
 
\t var $elementStatus = $('#status'); 
 
\t $elementStatus.html('processing...'); 
 
\t var url = 'my_parse_file.php'; 
 
\t var data = { firstname : $('#first_name').val() }; 
 
\t var success = function(responseText, textStatus, jqXHR) { 
 
\t \t if(jqXHR.status != 200) 
 
\t \t \t return; 
 

 
\t \t $elementStatus.html(responseText); 
 
\t }; 
 
\t $.post(url, data, success); 
 
}; 
 
\t </script> 
 
</head> 
 
<body> 
 
\t <h2>Ajax Post to PHP and Get Return Data</h2> 
 
\t <input type="hidden" name="my_id" value="1234"> 
 
\t First Name: <input id="first_name" name="first_name" type="text" onkeyup="ajax_post();"> <br><br> 
 
\t <div id="status"></div> 
 
</body> 
 
</html>