Prashanth Prashanth - 5 months ago 7
Javascript Question

How to display the querying 'DB' for get-variable with multiple values?

I am having a problem while displaying the value in the textbox.

I have check boxes that represent for applying job. When a user checks multiple check boxes and clicks the apply button, the

id
of the check box value is separated with
comma
and send to
applyView.php
file using ajax:

Example:
applyView.php?id=1,2,3


and the script is

$(document).ready(function(){
$('.job-apply').on('click', function(){
var selected = $('input[name="job[]"]:checked').map(function(i,e){return e.id;}).get();
var res = selected.join(",");
$.ajax({
url: 'php/applyView.php?id='+res,
dataType: 'json',
success: function(data){
if(data.length > 0)
{
var tmp = data[0];
$('#apply-title').attr('value',tmp.apply_title);
}
window.location.href = "apply.php";
}
});
});


And
applyView.php
file is:

require "dbconnect.php";
$id = $_REQUEST['id'];

$query = mysqli_query($conn, "SELECT `job_title` FROM `job` WHERE `job_id` IN ($id)");

$results = array();

while($row = mysqli_fetch_array($query))
{
$results[] = array(
'apply_title' => $row['job_title'],
);
}

$json = json_encode($results);

echo $json;


Output for the JSON Value is:

[{"apply_title":"Web developer"},{"apply_title":"Software Engineer"},{"apply_title":"Web developer"}]


And
apply.php
file is:

<form role="form" class="post-resume-form">
<div class="form-group">
<label for="job_title">Job Title</label>
<input type="Text" class="form-control input" id="apply-title" value="" />
</div>
</form>


Now the problem is to display only the value of the
id
(#apply-title)
with separated by
comma
is it possible.

In the form text box it has to be like this:

"Web Developer","Software Engineer","Web Developer"


Help me out guys!!

Answer

If I'm not mistaken, you want to take only the apply title, separated by commas. Instead of the echo(or beneath it) you can add this code:

$allTitles = "";
foreach($results as $applytitle){
   $allTitles = $allTitles . $applytitle["apply_title"] . ",";
}
echo $allTitles;

This should output Web Developer,Software Engineer,Web Developer,. The last comma(in the output) can be removed with a substring method.

NOTE: I haven't tested this, so it may not work! If you get an error, contact me

Comments