So i am using jquery, ajax, and php to query and show data from the database. I am submitting data to the ajax page as shown below.
Submit Ajax Request
$('#mainInput').keydown(function(e) {
if (e.keyCode == 13 || e.keyCode == 9) {
//Text after hitting enter button
var user_id = "<?php echo $user_id; ?>";
$.ajax({
type: 'POST',
url: 'ajax_get_code.php',
data: {mainInput: $("#mainInput").val(),
id : user_id},
datatype:"json",
success: function(response) {
var parsedData = jQuery.parseJSON(response);
$.each(parsedData, function(index){
if (parsedData.value2 == null){
$("#mainInput").val(parsedData.value);
$("#secondaryInput").val(parsedData.value);
}
else {
$("#mainInput").val(parsedData.value);
$("#secondaryInput").val(parsedData.value2);
}
});
},
error: function(xhr, status, error){
alert("Error!" + xhr.status);
}
});
e.preventDefault();
}
});
on the process request page I exploded the string to separate the submission based on this character ",". so that a user to run multiple queries at once.
Ajax Process request
<?php
require_once("class.codes.php");
$userCode= $_POST['mainInput'];
$user_id = $_POST['id'];
$removeSpace = str_replace(' ','', $userCode);
$parts = explode(",", $removeSpace);
foreach($parts as $part){
$all_codes = New CODES;
try
{
$stmt = $all_codes->runQueryTwo("SELECT user_code, user_text FROM codes WHERE user_code=:userCode AND user_id=:user_id");
$stmt->execute(array(':userCode'=>$part, ':user_id'=>$user_id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
//result
$user_text = $row['user_text'];
echo json_encode(array("value"=>$part,"value2"=>$user_text));
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
When I console log the request using the .parseJson library and only submitting one query I get the correct response and it fills in the textboxes accordingly. However when I submit multiple keys, which are separated by "," I don't get anything back. No error, no nothing.
However when I console log the request using multiple queries without using the .parseJson library I get the the correct response which looks like the sample data below.
{"value":"backbone","value2":"backbone, fixed."}{"value":"james","value2":"james"}{"value":"john","value2":null}
This is correct, but I now want to display all the the keys "value" in the mainInput textarea and the keys "value2" in the secondaryInput. It should loop through some how until there are no more key value pairs.
I have tried using the for loop and when I submit one query it returns double the key value pairs.
I am new to php and ajax please help as I have been researching and trying things all day. I have also tried a while loop and it just runs infinitely.
Suppose #mainInput
is a textarea, the line $("#mainInput").val(parsedData.value);
is overwriting every previous value in the for loop.
You must do is concatenate what is already in the textarea, hence in your ajax success function you would write you code as such:
var parsedData = JSON.parse(response);
$.each(parsedData, function(index, value){
$("#mainInput").val(parsedData.value);
$("#secondaryInput").val(parsedData.value);
});
Also rectified please note, the signature of $.each
callback function takes 2 parameters, first the index(this will be value
, value2
) then the value.