How to display data with radio buttons based on drop down selection?












1















I'm trying to dynamically generate radio buttons with data in front of them. The data that is to be displayed in front of the radio button is based on a drop down selection, which also displays some data in a text box using javascript.



I tried taking the selected option in a string and use it in the next query, but I know I am doing it wrong.



Database Connection



$db = pg_connect("");
$query = "select account_name,account_code,address1,address2,address3 FROM
customers";
$result = pg_query($db,$query);


//NEW QUERY



 $sql1= "select name from conferences";
$result1= pg_query($db, $sql1);


//END



//New Code



 <select class="form-control" id="conference"  name="conference">
<option value="">Select Conference...</option>
<?php while($rows1 = pg_fetch_assoc($result1)) { ?>
<option value="<?= $rows1['code']; ?>"><?= $rows1['name']; ?></option>
<?php } ?>
</select>
<br>


// END OF NEW CODE



Dropdown to select the data.



<select onchange="ChooseContact(this)" class="form-control" 
id="account_name" name="account_name" >

<?php
while($rows= pg_fetch_assoc($result)){
echo '<option value=" '.$rows['address1'].' '.$rows['address2'].'
'.$rows['address3'].''.$rows['account_code'].'">'.$rows['account_name'].'
'.$_POST[$rows['account_code']].'
</option>';
}?>
</select>


Displaying data in the text area based on the selcted value using javascript. (The code works fine till here)



<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;"value=""placeholder="Address...">
</textarea>
<script>
function ChooseContact(data) {
document.getElementById ("comment").value = data.value;
}
</script>


Displaying data in front of the radio buttons based on the selected option(This code works if I use some random value in the query, but not if I use the selected value 'account_code' from the previous query. I'm using POST GET method to carry the selected value)



 <?php


//NEW CODE



 $sql = "select  order_number, order_date from orders where 
customer_account_code = '3000614' and conference_code='DS19-'"; <-Data
gets displayed when put random value like this.

$code = $_GET[$rows['account_code']];
$conf = $_GET[$rows1['conference_code']];
$sql = "select order_number, order_date from orders where
customer_account_code = '$code' and conference_code= '$conf']"; <- But I
want to display the data against the selected value, i.e, the 'account_code'
in the variable $code from the dropdown select


//END



$res = pg_query($db,$sql);

while($value = pg_fetch_assoc($res) ){
echo "<input type='radio' name='answer'
value='".$value['order_number']." ".$value['order_date']."'>"
.$value['order_number'].$value['order_date']." </input><br />";
}
?>


I need to help to find a way to put the selected 'account_code' in a variable and use it in the $sql query.










share|improve this question

























  • please add in your question, the problem - tell me if your first code works for example :) and tell me if you use AJAX ?

    – Saif Manwill
    Jan 2 at 10:23













  • I have the updated the question, and no friend, I don't have much experience with ajax.

    – Cooper
    Jan 2 at 10:28











  • About 'account_code' in your question, you mean 'account_name' from select options ? you want to get account_name from selected option and run your query after submit ? for example ?

    – Saif Manwill
    Jan 2 at 10:43













  • No, the last the concatenated value in the option value=" . $rows['account_code']."

    – Cooper
    Jan 2 at 10:47











  • You want to get account_name from selected option and run your query after submitting? for example?

    – Saif Manwill
    Jan 2 at 10:48
















1















I'm trying to dynamically generate radio buttons with data in front of them. The data that is to be displayed in front of the radio button is based on a drop down selection, which also displays some data in a text box using javascript.



I tried taking the selected option in a string and use it in the next query, but I know I am doing it wrong.



Database Connection



$db = pg_connect("");
$query = "select account_name,account_code,address1,address2,address3 FROM
customers";
$result = pg_query($db,$query);


//NEW QUERY



 $sql1= "select name from conferences";
$result1= pg_query($db, $sql1);


//END



//New Code



 <select class="form-control" id="conference"  name="conference">
<option value="">Select Conference...</option>
<?php while($rows1 = pg_fetch_assoc($result1)) { ?>
<option value="<?= $rows1['code']; ?>"><?= $rows1['name']; ?></option>
<?php } ?>
</select>
<br>


// END OF NEW CODE



Dropdown to select the data.



<select onchange="ChooseContact(this)" class="form-control" 
id="account_name" name="account_name" >

<?php
while($rows= pg_fetch_assoc($result)){
echo '<option value=" '.$rows['address1'].' '.$rows['address2'].'
'.$rows['address3'].''.$rows['account_code'].'">'.$rows['account_name'].'
'.$_POST[$rows['account_code']].'
</option>';
}?>
</select>


Displaying data in the text area based on the selcted value using javascript. (The code works fine till here)



<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;"value=""placeholder="Address...">
</textarea>
<script>
function ChooseContact(data) {
document.getElementById ("comment").value = data.value;
}
</script>


Displaying data in front of the radio buttons based on the selected option(This code works if I use some random value in the query, but not if I use the selected value 'account_code' from the previous query. I'm using POST GET method to carry the selected value)



 <?php


//NEW CODE



 $sql = "select  order_number, order_date from orders where 
customer_account_code = '3000614' and conference_code='DS19-'"; <-Data
gets displayed when put random value like this.

$code = $_GET[$rows['account_code']];
$conf = $_GET[$rows1['conference_code']];
$sql = "select order_number, order_date from orders where
customer_account_code = '$code' and conference_code= '$conf']"; <- But I
want to display the data against the selected value, i.e, the 'account_code'
in the variable $code from the dropdown select


//END



$res = pg_query($db,$sql);

while($value = pg_fetch_assoc($res) ){
echo "<input type='radio' name='answer'
value='".$value['order_number']." ".$value['order_date']."'>"
.$value['order_number'].$value['order_date']." </input><br />";
}
?>


I need to help to find a way to put the selected 'account_code' in a variable and use it in the $sql query.










share|improve this question

























  • please add in your question, the problem - tell me if your first code works for example :) and tell me if you use AJAX ?

    – Saif Manwill
    Jan 2 at 10:23













  • I have the updated the question, and no friend, I don't have much experience with ajax.

    – Cooper
    Jan 2 at 10:28











  • About 'account_code' in your question, you mean 'account_name' from select options ? you want to get account_name from selected option and run your query after submit ? for example ?

    – Saif Manwill
    Jan 2 at 10:43













  • No, the last the concatenated value in the option value=" . $rows['account_code']."

    – Cooper
    Jan 2 at 10:47











  • You want to get account_name from selected option and run your query after submitting? for example?

    – Saif Manwill
    Jan 2 at 10:48














1












1








1








I'm trying to dynamically generate radio buttons with data in front of them. The data that is to be displayed in front of the radio button is based on a drop down selection, which also displays some data in a text box using javascript.



I tried taking the selected option in a string and use it in the next query, but I know I am doing it wrong.



Database Connection



$db = pg_connect("");
$query = "select account_name,account_code,address1,address2,address3 FROM
customers";
$result = pg_query($db,$query);


//NEW QUERY



 $sql1= "select name from conferences";
$result1= pg_query($db, $sql1);


//END



//New Code



 <select class="form-control" id="conference"  name="conference">
<option value="">Select Conference...</option>
<?php while($rows1 = pg_fetch_assoc($result1)) { ?>
<option value="<?= $rows1['code']; ?>"><?= $rows1['name']; ?></option>
<?php } ?>
</select>
<br>


// END OF NEW CODE



Dropdown to select the data.



<select onchange="ChooseContact(this)" class="form-control" 
id="account_name" name="account_name" >

<?php
while($rows= pg_fetch_assoc($result)){
echo '<option value=" '.$rows['address1'].' '.$rows['address2'].'
'.$rows['address3'].''.$rows['account_code'].'">'.$rows['account_name'].'
'.$_POST[$rows['account_code']].'
</option>';
}?>
</select>


Displaying data in the text area based on the selcted value using javascript. (The code works fine till here)



<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;"value=""placeholder="Address...">
</textarea>
<script>
function ChooseContact(data) {
document.getElementById ("comment").value = data.value;
}
</script>


Displaying data in front of the radio buttons based on the selected option(This code works if I use some random value in the query, but not if I use the selected value 'account_code' from the previous query. I'm using POST GET method to carry the selected value)



 <?php


//NEW CODE



 $sql = "select  order_number, order_date from orders where 
customer_account_code = '3000614' and conference_code='DS19-'"; <-Data
gets displayed when put random value like this.

$code = $_GET[$rows['account_code']];
$conf = $_GET[$rows1['conference_code']];
$sql = "select order_number, order_date from orders where
customer_account_code = '$code' and conference_code= '$conf']"; <- But I
want to display the data against the selected value, i.e, the 'account_code'
in the variable $code from the dropdown select


//END



$res = pg_query($db,$sql);

while($value = pg_fetch_assoc($res) ){
echo "<input type='radio' name='answer'
value='".$value['order_number']." ".$value['order_date']."'>"
.$value['order_number'].$value['order_date']." </input><br />";
}
?>


I need to help to find a way to put the selected 'account_code' in a variable and use it in the $sql query.










share|improve this question
















I'm trying to dynamically generate radio buttons with data in front of them. The data that is to be displayed in front of the radio button is based on a drop down selection, which also displays some data in a text box using javascript.



I tried taking the selected option in a string and use it in the next query, but I know I am doing it wrong.



Database Connection



$db = pg_connect("");
$query = "select account_name,account_code,address1,address2,address3 FROM
customers";
$result = pg_query($db,$query);


//NEW QUERY



 $sql1= "select name from conferences";
$result1= pg_query($db, $sql1);


//END



//New Code



 <select class="form-control" id="conference"  name="conference">
<option value="">Select Conference...</option>
<?php while($rows1 = pg_fetch_assoc($result1)) { ?>
<option value="<?= $rows1['code']; ?>"><?= $rows1['name']; ?></option>
<?php } ?>
</select>
<br>


// END OF NEW CODE



Dropdown to select the data.



<select onchange="ChooseContact(this)" class="form-control" 
id="account_name" name="account_name" >

<?php
while($rows= pg_fetch_assoc($result)){
echo '<option value=" '.$rows['address1'].' '.$rows['address2'].'
'.$rows['address3'].''.$rows['account_code'].'">'.$rows['account_name'].'
'.$_POST[$rows['account_code']].'
</option>';
}?>
</select>


Displaying data in the text area based on the selcted value using javascript. (The code works fine till here)



<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;"value=""placeholder="Address...">
</textarea>
<script>
function ChooseContact(data) {
document.getElementById ("comment").value = data.value;
}
</script>


Displaying data in front of the radio buttons based on the selected option(This code works if I use some random value in the query, but not if I use the selected value 'account_code' from the previous query. I'm using POST GET method to carry the selected value)



 <?php


//NEW CODE



 $sql = "select  order_number, order_date from orders where 
customer_account_code = '3000614' and conference_code='DS19-'"; <-Data
gets displayed when put random value like this.

$code = $_GET[$rows['account_code']];
$conf = $_GET[$rows1['conference_code']];
$sql = "select order_number, order_date from orders where
customer_account_code = '$code' and conference_code= '$conf']"; <- But I
want to display the data against the selected value, i.e, the 'account_code'
in the variable $code from the dropdown select


//END



$res = pg_query($db,$sql);

while($value = pg_fetch_assoc($res) ){
echo "<input type='radio' name='answer'
value='".$value['order_number']." ".$value['order_date']."'>"
.$value['order_number'].$value['order_date']." </input><br />";
}
?>


I need to help to find a way to put the selected 'account_code' in a variable and use it in the $sql query.







javascript php postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 12:51







Cooper

















asked Jan 2 at 10:17









CooperCooper

277




277













  • please add in your question, the problem - tell me if your first code works for example :) and tell me if you use AJAX ?

    – Saif Manwill
    Jan 2 at 10:23













  • I have the updated the question, and no friend, I don't have much experience with ajax.

    – Cooper
    Jan 2 at 10:28











  • About 'account_code' in your question, you mean 'account_name' from select options ? you want to get account_name from selected option and run your query after submit ? for example ?

    – Saif Manwill
    Jan 2 at 10:43













  • No, the last the concatenated value in the option value=" . $rows['account_code']."

    – Cooper
    Jan 2 at 10:47











  • You want to get account_name from selected option and run your query after submitting? for example?

    – Saif Manwill
    Jan 2 at 10:48



















  • please add in your question, the problem - tell me if your first code works for example :) and tell me if you use AJAX ?

    – Saif Manwill
    Jan 2 at 10:23













  • I have the updated the question, and no friend, I don't have much experience with ajax.

    – Cooper
    Jan 2 at 10:28











  • About 'account_code' in your question, you mean 'account_name' from select options ? you want to get account_name from selected option and run your query after submit ? for example ?

    – Saif Manwill
    Jan 2 at 10:43













  • No, the last the concatenated value in the option value=" . $rows['account_code']."

    – Cooper
    Jan 2 at 10:47











  • You want to get account_name from selected option and run your query after submitting? for example?

    – Saif Manwill
    Jan 2 at 10:48

















please add in your question, the problem - tell me if your first code works for example :) and tell me if you use AJAX ?

– Saif Manwill
Jan 2 at 10:23







please add in your question, the problem - tell me if your first code works for example :) and tell me if you use AJAX ?

– Saif Manwill
Jan 2 at 10:23















I have the updated the question, and no friend, I don't have much experience with ajax.

– Cooper
Jan 2 at 10:28





I have the updated the question, and no friend, I don't have much experience with ajax.

– Cooper
Jan 2 at 10:28













About 'account_code' in your question, you mean 'account_name' from select options ? you want to get account_name from selected option and run your query after submit ? for example ?

– Saif Manwill
Jan 2 at 10:43







About 'account_code' in your question, you mean 'account_name' from select options ? you want to get account_name from selected option and run your query after submit ? for example ?

– Saif Manwill
Jan 2 at 10:43















No, the last the concatenated value in the option value=" . $rows['account_code']."

– Cooper
Jan 2 at 10:47





No, the last the concatenated value in the option value=" . $rows['account_code']."

– Cooper
Jan 2 at 10:47













You want to get account_name from selected option and run your query after submitting? for example?

– Saif Manwill
Jan 2 at 10:48





You want to get account_name from selected option and run your query after submitting? for example?

– Saif Manwill
Jan 2 at 10:48












1 Answer
1






active

oldest

votes


















0














Please try with this code : (It's work for me)



1- Add this line to your HTML <head>:



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>


2- Edit your CODE to this:



Dropdown to select the data:



<select class="form-control" id="account_name"  name="account_name">
<option value=""></option>
<?php while($rows = pg_fetch_assoc($result)) { ?>
<option value="<?= $rows['address1'].' '.$rows['address2'].' '.$rows['address3'].'-'.$rows['account_code']; ?>"><?= $rows['account_name']; ?></option>
<? } ?>
</select>


Displaying data in the text area based on the selected value using jQuery:



<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;" value="" placeholder="Address..."></textarea>


jQuery Code:



<script type="text/javascript">
$('#comment').val($('#account_name').val()); // MAKE A DEFAULT VALUE
(function($) {
$('#account_name').change(function() {
$('#results').html(''); // REMOVE THE OLD RESULTS
var option = $(this).val();
$('#comment').val(option);
// EDIT RADIO WITH AJAX
$.ajax({
type: "POST",
url: "path/test.php",
dataType:'JSON',
data: $('#account_name').serialize()
}).done(function(data) {
for (var i = 0; i < data.length; i++) {
// ADD RADIO TO DIV RESULTS
$('#results').append('<input type="radio" name="answer" value="'+data[i].order_number+'">'+data[i].order_date+'</input><br>');
}
});
});
})(jQuery);
</script>


after that, add this HTML to your page, to show RESULTS FROM AJAX DATA



<!-- RADIOs -->
<div id="results"></div>


3- Create a new file like path/test.php



in this file, use this CODE to return values with JSON :)



<?php
header('Content-type: application/json');

// CONNECT (JUST USE YOUR CUSTOM CONNECTION METHOD & REQUIRE CONFIG FILE IF YOU WANT)
$db = pg_connect("");

$value = explode('-', $_POST['account_name']);

// EXPLODE AND GET LAST NUMBER AFTER < - >
$code = (int) end($value);

$sql = "select order_number, order_date from orders where customer_account_code = '$code'";

$res = pg_query($db, $sql);

// CREATE JSON RESULTS
$is = '';
while($data = pg_fetch_assoc($res)) {
$is .= json_encode($data).', ';
}

// AND GET ALL
echo '['.substr($is, 0, -2).']';
?>





share|improve this answer


























  • Hi. I tried the code, it's not working :( There is no error message, but the radio buttons are not getting displayed.

    – Cooper
    Jan 2 at 14:12













  • I think from SQL, I've updated this code - please try and use your connection settings - Try to open your path/test.php and use a $code = 2; for example and see if you get any data ;)

    – Saif Manwill
    Jan 2 at 18:10











  • Please re-check my code, It's work but makes sure you use the correct path here, because I've tested, & everything GOOD, If it's work, please tell me, It took me a lot of time, I hope it works and helps you :)

    – Saif Manwill
    Jan 2 at 20:04













  • For the second time, I've tested & with your question data like: Date, id .... ;)

    – Saif Manwill
    Jan 2 at 20:14













  • Hi Sir. I'm really sorry I couldn't revert. My work hours were over. I'm working on the code you wrote. Really appreciate your help :)

    – Cooper
    Jan 3 at 5:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004534%2fhow-to-display-data-with-radio-buttons-based-on-drop-down-selection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Please try with this code : (It's work for me)



1- Add this line to your HTML <head>:



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>


2- Edit your CODE to this:



Dropdown to select the data:



<select class="form-control" id="account_name"  name="account_name">
<option value=""></option>
<?php while($rows = pg_fetch_assoc($result)) { ?>
<option value="<?= $rows['address1'].' '.$rows['address2'].' '.$rows['address3'].'-'.$rows['account_code']; ?>"><?= $rows['account_name']; ?></option>
<? } ?>
</select>


Displaying data in the text area based on the selected value using jQuery:



<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;" value="" placeholder="Address..."></textarea>


jQuery Code:



<script type="text/javascript">
$('#comment').val($('#account_name').val()); // MAKE A DEFAULT VALUE
(function($) {
$('#account_name').change(function() {
$('#results').html(''); // REMOVE THE OLD RESULTS
var option = $(this).val();
$('#comment').val(option);
// EDIT RADIO WITH AJAX
$.ajax({
type: "POST",
url: "path/test.php",
dataType:'JSON',
data: $('#account_name').serialize()
}).done(function(data) {
for (var i = 0; i < data.length; i++) {
// ADD RADIO TO DIV RESULTS
$('#results').append('<input type="radio" name="answer" value="'+data[i].order_number+'">'+data[i].order_date+'</input><br>');
}
});
});
})(jQuery);
</script>


after that, add this HTML to your page, to show RESULTS FROM AJAX DATA



<!-- RADIOs -->
<div id="results"></div>


3- Create a new file like path/test.php



in this file, use this CODE to return values with JSON :)



<?php
header('Content-type: application/json');

// CONNECT (JUST USE YOUR CUSTOM CONNECTION METHOD & REQUIRE CONFIG FILE IF YOU WANT)
$db = pg_connect("");

$value = explode('-', $_POST['account_name']);

// EXPLODE AND GET LAST NUMBER AFTER < - >
$code = (int) end($value);

$sql = "select order_number, order_date from orders where customer_account_code = '$code'";

$res = pg_query($db, $sql);

// CREATE JSON RESULTS
$is = '';
while($data = pg_fetch_assoc($res)) {
$is .= json_encode($data).', ';
}

// AND GET ALL
echo '['.substr($is, 0, -2).']';
?>





share|improve this answer


























  • Hi. I tried the code, it's not working :( There is no error message, but the radio buttons are not getting displayed.

    – Cooper
    Jan 2 at 14:12













  • I think from SQL, I've updated this code - please try and use your connection settings - Try to open your path/test.php and use a $code = 2; for example and see if you get any data ;)

    – Saif Manwill
    Jan 2 at 18:10











  • Please re-check my code, It's work but makes sure you use the correct path here, because I've tested, & everything GOOD, If it's work, please tell me, It took me a lot of time, I hope it works and helps you :)

    – Saif Manwill
    Jan 2 at 20:04













  • For the second time, I've tested & with your question data like: Date, id .... ;)

    – Saif Manwill
    Jan 2 at 20:14













  • Hi Sir. I'm really sorry I couldn't revert. My work hours were over. I'm working on the code you wrote. Really appreciate your help :)

    – Cooper
    Jan 3 at 5:17
















0














Please try with this code : (It's work for me)



1- Add this line to your HTML <head>:



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>


2- Edit your CODE to this:



Dropdown to select the data:



<select class="form-control" id="account_name"  name="account_name">
<option value=""></option>
<?php while($rows = pg_fetch_assoc($result)) { ?>
<option value="<?= $rows['address1'].' '.$rows['address2'].' '.$rows['address3'].'-'.$rows['account_code']; ?>"><?= $rows['account_name']; ?></option>
<? } ?>
</select>


Displaying data in the text area based on the selected value using jQuery:



<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;" value="" placeholder="Address..."></textarea>


jQuery Code:



<script type="text/javascript">
$('#comment').val($('#account_name').val()); // MAKE A DEFAULT VALUE
(function($) {
$('#account_name').change(function() {
$('#results').html(''); // REMOVE THE OLD RESULTS
var option = $(this).val();
$('#comment').val(option);
// EDIT RADIO WITH AJAX
$.ajax({
type: "POST",
url: "path/test.php",
dataType:'JSON',
data: $('#account_name').serialize()
}).done(function(data) {
for (var i = 0; i < data.length; i++) {
// ADD RADIO TO DIV RESULTS
$('#results').append('<input type="radio" name="answer" value="'+data[i].order_number+'">'+data[i].order_date+'</input><br>');
}
});
});
})(jQuery);
</script>


after that, add this HTML to your page, to show RESULTS FROM AJAX DATA



<!-- RADIOs -->
<div id="results"></div>


3- Create a new file like path/test.php



in this file, use this CODE to return values with JSON :)



<?php
header('Content-type: application/json');

// CONNECT (JUST USE YOUR CUSTOM CONNECTION METHOD & REQUIRE CONFIG FILE IF YOU WANT)
$db = pg_connect("");

$value = explode('-', $_POST['account_name']);

// EXPLODE AND GET LAST NUMBER AFTER < - >
$code = (int) end($value);

$sql = "select order_number, order_date from orders where customer_account_code = '$code'";

$res = pg_query($db, $sql);

// CREATE JSON RESULTS
$is = '';
while($data = pg_fetch_assoc($res)) {
$is .= json_encode($data).', ';
}

// AND GET ALL
echo '['.substr($is, 0, -2).']';
?>





share|improve this answer


























  • Hi. I tried the code, it's not working :( There is no error message, but the radio buttons are not getting displayed.

    – Cooper
    Jan 2 at 14:12













  • I think from SQL, I've updated this code - please try and use your connection settings - Try to open your path/test.php and use a $code = 2; for example and see if you get any data ;)

    – Saif Manwill
    Jan 2 at 18:10











  • Please re-check my code, It's work but makes sure you use the correct path here, because I've tested, & everything GOOD, If it's work, please tell me, It took me a lot of time, I hope it works and helps you :)

    – Saif Manwill
    Jan 2 at 20:04













  • For the second time, I've tested & with your question data like: Date, id .... ;)

    – Saif Manwill
    Jan 2 at 20:14













  • Hi Sir. I'm really sorry I couldn't revert. My work hours were over. I'm working on the code you wrote. Really appreciate your help :)

    – Cooper
    Jan 3 at 5:17














0












0








0







Please try with this code : (It's work for me)



1- Add this line to your HTML <head>:



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>


2- Edit your CODE to this:



Dropdown to select the data:



<select class="form-control" id="account_name"  name="account_name">
<option value=""></option>
<?php while($rows = pg_fetch_assoc($result)) { ?>
<option value="<?= $rows['address1'].' '.$rows['address2'].' '.$rows['address3'].'-'.$rows['account_code']; ?>"><?= $rows['account_name']; ?></option>
<? } ?>
</select>


Displaying data in the text area based on the selected value using jQuery:



<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;" value="" placeholder="Address..."></textarea>


jQuery Code:



<script type="text/javascript">
$('#comment').val($('#account_name').val()); // MAKE A DEFAULT VALUE
(function($) {
$('#account_name').change(function() {
$('#results').html(''); // REMOVE THE OLD RESULTS
var option = $(this).val();
$('#comment').val(option);
// EDIT RADIO WITH AJAX
$.ajax({
type: "POST",
url: "path/test.php",
dataType:'JSON',
data: $('#account_name').serialize()
}).done(function(data) {
for (var i = 0; i < data.length; i++) {
// ADD RADIO TO DIV RESULTS
$('#results').append('<input type="radio" name="answer" value="'+data[i].order_number+'">'+data[i].order_date+'</input><br>');
}
});
});
})(jQuery);
</script>


after that, add this HTML to your page, to show RESULTS FROM AJAX DATA



<!-- RADIOs -->
<div id="results"></div>


3- Create a new file like path/test.php



in this file, use this CODE to return values with JSON :)



<?php
header('Content-type: application/json');

// CONNECT (JUST USE YOUR CUSTOM CONNECTION METHOD & REQUIRE CONFIG FILE IF YOU WANT)
$db = pg_connect("");

$value = explode('-', $_POST['account_name']);

// EXPLODE AND GET LAST NUMBER AFTER < - >
$code = (int) end($value);

$sql = "select order_number, order_date from orders where customer_account_code = '$code'";

$res = pg_query($db, $sql);

// CREATE JSON RESULTS
$is = '';
while($data = pg_fetch_assoc($res)) {
$is .= json_encode($data).', ';
}

// AND GET ALL
echo '['.substr($is, 0, -2).']';
?>





share|improve this answer















Please try with this code : (It's work for me)



1- Add this line to your HTML <head>:



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>


2- Edit your CODE to this:



Dropdown to select the data:



<select class="form-control" id="account_name"  name="account_name">
<option value=""></option>
<?php while($rows = pg_fetch_assoc($result)) { ?>
<option value="<?= $rows['address1'].' '.$rows['address2'].' '.$rows['address3'].'-'.$rows['account_code']; ?>"><?= $rows['account_name']; ?></option>
<? } ?>
</select>


Displaying data in the text area based on the selected value using jQuery:



<textarea readonly class="form-control" style="background-color: #F5F5F5;" 
id="comment" rows="5" style="width:700px;" value="" placeholder="Address..."></textarea>


jQuery Code:



<script type="text/javascript">
$('#comment').val($('#account_name').val()); // MAKE A DEFAULT VALUE
(function($) {
$('#account_name').change(function() {
$('#results').html(''); // REMOVE THE OLD RESULTS
var option = $(this).val();
$('#comment').val(option);
// EDIT RADIO WITH AJAX
$.ajax({
type: "POST",
url: "path/test.php",
dataType:'JSON',
data: $('#account_name').serialize()
}).done(function(data) {
for (var i = 0; i < data.length; i++) {
// ADD RADIO TO DIV RESULTS
$('#results').append('<input type="radio" name="answer" value="'+data[i].order_number+'">'+data[i].order_date+'</input><br>');
}
});
});
})(jQuery);
</script>


after that, add this HTML to your page, to show RESULTS FROM AJAX DATA



<!-- RADIOs -->
<div id="results"></div>


3- Create a new file like path/test.php



in this file, use this CODE to return values with JSON :)



<?php
header('Content-type: application/json');

// CONNECT (JUST USE YOUR CUSTOM CONNECTION METHOD & REQUIRE CONFIG FILE IF YOU WANT)
$db = pg_connect("");

$value = explode('-', $_POST['account_name']);

// EXPLODE AND GET LAST NUMBER AFTER < - >
$code = (int) end($value);

$sql = "select order_number, order_date from orders where customer_account_code = '$code'";

$res = pg_query($db, $sql);

// CREATE JSON RESULTS
$is = '';
while($data = pg_fetch_assoc($res)) {
$is .= json_encode($data).', ';
}

// AND GET ALL
echo '['.substr($is, 0, -2).']';
?>






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 21:56

























answered Jan 2 at 12:58









Saif ManwillSaif Manwill

198110




198110













  • Hi. I tried the code, it's not working :( There is no error message, but the radio buttons are not getting displayed.

    – Cooper
    Jan 2 at 14:12













  • I think from SQL, I've updated this code - please try and use your connection settings - Try to open your path/test.php and use a $code = 2; for example and see if you get any data ;)

    – Saif Manwill
    Jan 2 at 18:10











  • Please re-check my code, It's work but makes sure you use the correct path here, because I've tested, & everything GOOD, If it's work, please tell me, It took me a lot of time, I hope it works and helps you :)

    – Saif Manwill
    Jan 2 at 20:04













  • For the second time, I've tested & with your question data like: Date, id .... ;)

    – Saif Manwill
    Jan 2 at 20:14













  • Hi Sir. I'm really sorry I couldn't revert. My work hours were over. I'm working on the code you wrote. Really appreciate your help :)

    – Cooper
    Jan 3 at 5:17



















  • Hi. I tried the code, it's not working :( There is no error message, but the radio buttons are not getting displayed.

    – Cooper
    Jan 2 at 14:12













  • I think from SQL, I've updated this code - please try and use your connection settings - Try to open your path/test.php and use a $code = 2; for example and see if you get any data ;)

    – Saif Manwill
    Jan 2 at 18:10











  • Please re-check my code, It's work but makes sure you use the correct path here, because I've tested, & everything GOOD, If it's work, please tell me, It took me a lot of time, I hope it works and helps you :)

    – Saif Manwill
    Jan 2 at 20:04













  • For the second time, I've tested & with your question data like: Date, id .... ;)

    – Saif Manwill
    Jan 2 at 20:14













  • Hi Sir. I'm really sorry I couldn't revert. My work hours were over. I'm working on the code you wrote. Really appreciate your help :)

    – Cooper
    Jan 3 at 5:17

















Hi. I tried the code, it's not working :( There is no error message, but the radio buttons are not getting displayed.

– Cooper
Jan 2 at 14:12







Hi. I tried the code, it's not working :( There is no error message, but the radio buttons are not getting displayed.

– Cooper
Jan 2 at 14:12















I think from SQL, I've updated this code - please try and use your connection settings - Try to open your path/test.php and use a $code = 2; for example and see if you get any data ;)

– Saif Manwill
Jan 2 at 18:10





I think from SQL, I've updated this code - please try and use your connection settings - Try to open your path/test.php and use a $code = 2; for example and see if you get any data ;)

– Saif Manwill
Jan 2 at 18:10













Please re-check my code, It's work but makes sure you use the correct path here, because I've tested, & everything GOOD, If it's work, please tell me, It took me a lot of time, I hope it works and helps you :)

– Saif Manwill
Jan 2 at 20:04







Please re-check my code, It's work but makes sure you use the correct path here, because I've tested, & everything GOOD, If it's work, please tell me, It took me a lot of time, I hope it works and helps you :)

– Saif Manwill
Jan 2 at 20:04















For the second time, I've tested & with your question data like: Date, id .... ;)

– Saif Manwill
Jan 2 at 20:14







For the second time, I've tested & with your question data like: Date, id .... ;)

– Saif Manwill
Jan 2 at 20:14















Hi Sir. I'm really sorry I couldn't revert. My work hours were over. I'm working on the code you wrote. Really appreciate your help :)

– Cooper
Jan 3 at 5:17





Hi Sir. I'm really sorry I couldn't revert. My work hours were over. I'm working on the code you wrote. Really appreciate your help :)

– Cooper
Jan 3 at 5:17




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004534%2fhow-to-display-data-with-radio-buttons-based-on-drop-down-selection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

ts Property 'filter' does not exist on type '{}'

mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window