Jump to content

return value from ajax call doesn't display in my html input tag


Cheslia

Recommended Posts

Edited by Psycho: Please use code tags

 

I have a php program containing this script tag....

 

<script>
        $(document).ready(function() {
            $("#csid").on("click", function () {
                console.log($("#csid").val());
                var cs_id = $("#csid").val();
                var schema_name = '<?php echo $schema ?>';
                console.log(schema_name);
                console.log(cs_id);
                $.ajax({
                type: 'Post',
                    url: 'display_custext.php',
                data: {
                    csid:cs_id,                                //passes the variable cs_id as the $_POST['csid'] to the designated page
                    schema:schema_name,          //passes the variable schema_name as the $_POST['schema'] to the designated page
                },
                success: function (data) {
                    var obj = $.parseJSON(data);
                    var result = "";
                    $.each(obj, function() {
                        result = result + this['PROTOCOL_STEP_CUSTOM_TEXT'] + "<br>";     //create what will be passed to the .html tag in the ID='newSection' tagged section of html
                    });
                    $('#newSection').html(result);        //returns the value to display in the <div> tag
                }
            });
            });
        });
    </script>
 
 
then in the body of my php program I have this section....it will populate the <div> but not the <input> tag.  
 
                <p>
                    Select NEW or an existing Custom Text Script:<br><br>
                    <select size="7" name="text_choices[]" class="select" id="csid">
                        <?php $page->get_custext($schema); ?>
                    </select><br><br>
                </p>
                <div id="newSection">goes here</div><br><br>
                <input type="text" id="newSection" class="txtbox"><br><br>

 

this is the display_custest.php code the goes to the database and pulls the data to display....this works fine as well.

 

<?php
include_once('includes/config.php');
$dbh_oracle = oci_connect(ORACLE_USER, ORACLE_PASS, ORACLE_NAME);
 
$csid = $_POST['csid'];
$schema = $_POST['schema'];
 
 
$myquery = "select ps.protocol_step_id, PS.PROTOCOL_STEP_CUSTOM_TEXT
            from " . $schema . ".PROTOCOL_STEP ps  
            where ps.protocol_step_id = " . $csid . "
            order by PS.PROTOCOL_STEP_DESC";
$sth = oci_parse($dbh_oracle, $myquery);
$results = oci_execute($sth);
$cus_text = "";
if (!$results) {
    echo "Could not get the custom text for the protocol step id - " . oci_error();
} else {
    while($row = oci_fetch_array($sth, OCI_ASSOC)) {
        $cus_text[]=$row;
    }
}
echo json_encode($cus_text);
 
?>

 

 

Thanks in advance.

Edited by Psycho
Link to comment
Share on other sites

First, you can't have two elements on a page that have the same ID. The purpose of an ID is to identify a specific element.

 

Second, you can't use .html() to set the value of an input field. That method is used to set the HTML content between tags such as DIV, P, SPAN, etc. An input field does not have HTML content - it has a value. So, you need something like this

 

$('#newSection').html(result);
$('#inputSectionID').val(result);
Link to comment
Share on other sites

Oops, sorry about not encapsulating the code properly, I see that now and will do in the future.  I was aware of not having two elements on the page with the same ID,  I was removing/adding them back and forth in testing, but just copied them both in the code snippet.  I should have clarified.  

 

And your solution $('#inputSectionID').val(result); worked perfectly, thanks!  I knew it was probably something simple I was missing.  Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.