-3

I have a select element which has values of sequences in a database:

<option value="1">one</option>
<option value="2">two</option>

I then have this PHP code which puts the table data into an array

<?php
$return_arr = array();
$sql="SELECT * from tickets_standardresponses ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs)) {
    $return_arr[] = array('sequence' => $result["sequence"], 'response' => $result["response"]);
}
$data = json_encode($return_arr);
?>
<script type="text/javascript">
$(document).ready(function(){
    var data = <?php echo $data; ?>;
    $('#standard_response').on('change',function() {

    });
});
</script>

How can I fill a textarea based on the selected option

For example, if the selected option's value = 1 i want to put the response data from the array where the sequence = 1

I do not want to use GET/POST requests using Ajax

charlie
  • 415
  • 4
  • 35
  • 83
  • i am not using AJAX for GET/POST requests though so i dont see this as a duplicate – charlie Sep 30 '15 at 17:12
  • Possible duplicate of [Insert text into textarea with jQuery](http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery) – David Sep 30 '15 at 17:15
  • i don't see a `textarea` here. but you can just do `$('#standard_response').on('change',function() { $('#myTextAreaId').text($(this).val()); });` – Sushil Sep 30 '15 at 17:15
  • its not going to be the value of the selected option, it will be the data from the JQuery array generated by the PHP code – charlie Sep 30 '15 at 17:16
  • If you're not using ajax then you just want share in your question the relevant portion of the generated HTML and the JS you have so far. No need of sharing PHP especially if we cannot see its relevance to your question. – PeterKA Sep 30 '15 at 17:16
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Sep 30 '15 at 17:17
  • @charlie: Then use a value from the `data` variable in the code. It's not clear what the problem is. – David Sep 30 '15 at 17:17
  • @David thats what i want to do, but i want to know how to get the correct value from the array based on the selected "sequence" number – charlie Sep 30 '15 at 17:19
  • @charlie: Well, what's *in* the `data` variable? We don't know what the "correct" value is. Presumably there's some object in that variable. Examine it in the debugger to see what it contains. Use some combination of logical conditions to determine the value you want and use that value. – David Sep 30 '15 at 17:20
  • check the question, the data array contains sequence and response. sequence is a number and response is a block of text – charlie Sep 30 '15 at 17:21
  • @charlie, we are mere humans :) Although we understand PHP, we cannot process it to produce the HTML that your jQuery will act on. ... A little more work on your part will make your question much easier to answer. From the browser rendering of the *data array*, extract a sample and post in your question. And it appears that the way you're outputting the data is not consistent with the way you want to use it. You probably want to use a script block to output the data into a JS variable. Then you'll have the data in memory once your page loads. – PeterKA Sep 30 '15 at 17:24
  • @charlie: The question shows server-side code which generates the value in that variable. It doesn't show what's *actually* in that variable. We can't debug this in your browser for you. Presumably there's some value in the structure of that variable which you want to use. Look at the data in that variable at runtime and see where that value is. Then write some logic to get that value. This may involve things like `if` statements and/or `for` loops. For example, looping over an array to find a matching value and then using another value on that matching object. – David Sep 30 '15 at 17:25

3 Answers3

1
        var data = <?php echo $data; ?>;

        $('#response').on('change', function() {

            var sequence = $(this).val();
            //Check each object in the data array
            $.each(data, function( index, obj ) {
                if (obj.sequence === sequence) {
                    //The "result" textarea
                    $('#result').text(obj.response);
                }
            });
        });

You have to loop through the data array and check each objects sequence and see if it matches the value of the selected option.

Note: Make sure that the sequence parameter is a string or convert the option value to a integer.

John Svensson
  • 392
  • 1
  • 6
1

This is untested but should get you close. Using $.grep will search your data array for any objects that match the sequence and return those objects to the matches array. Since there should only be one. you can just output the first matches response.

$('#standard_response').on('change',function() {
    var seq = $(this).val();
    var matches = $.grep(data, function(e) { return e.sequence == seq });
    if(matches.length > 0){
        $('textarea').text(matches[0].response);
    }
});
Gregg Duncan
  • 2,635
  • 1
  • 14
  • 20
0

Since, Dropdown Is not visible completely. I assumed class name as 'SequenceSelect', which is also used in script tag. So, if you are changing class name of select dropdown, change in script tag too.

<select class='SequenceSelect'>
    <option value="1">one</option>
    <option value="2">two</option>
</select>

<div class="ShowSequenceResponse">
    <textarea name="SequenceRespeonse"></textarea>
</div>

<script>
    $('.SequenceSelect').change(function(){
        var SequenceNo= $('.SequenceSelect').val();
        $.ajax({url:"AjaxSequenceSelect.php?SequenceNo="+SequenceNo,cache:false,success:function(result){
            $('#ShowSequenceResponse').html(result);
        }});
    });
</script>

create one page AjaxSequenceSelect.php (this page is used in script tag, so pay attention here.)

<?php

extract($_GET);

$sql="SELECT * from tickets_standardresponses WHERE sequence='$SequenceNo'";
$rs=mysql_query($sql,$conn);
while($result=mysql_fetch_array($rs))
{
    $Response=$result['response'];
}
?>

<textarea name="SequenceRespeonse"><?echo $Response;?></textarea>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77