asvasdv asdvsadvs
asvasdv asdvsadvs

Reputation: 113

Jquery show/hide element based on mysql selection

I wanted to show/hide element based on MySql Value

$(document).bind('DOMNodeInserted', '#composeHeaderTable0', function(event) {
    if ($('#data_parent_type0').val() == 'Leads') {
    $("#email_template0 option[value='151ddf5a-3fe8-84bd-50db-533545893c67']").hide();
    $("#email_template0 option[value='15a6040b-bbad-1a89-d62b-537de2b73832']").show();
}
    if ($('#data_parent_type0').val() == 'Contacts') {
    $("#email_template0 option[value='1b560788-07b2-0798-6b09-5335520bd9df']").hide();
    $("#email_template0 option[value='f15bf4b4-d174-f0d6-6f09-537c8d3e9693']").show();

}
    return false;
});

Above script works, but I need to show hide based on Mysql call: I have partial php corresponding file

<?php
    mysql_connect('mysql', 'admin', '123');
    mysql_select_db('mydb');
    $Leads0emailAddress0 = mysql_real_escape_string($_POST['Leads0emailAddress0']);
    $result = mysql_query('select id from email_templates where description = 'Customers' and deleted = 0;');
...............
?>

Upvotes: 0

Views: 610

Answers (2)

Saqueib
Saqueib

Reputation: 3520

Your mysql script has error

    $result = mysql_query('select id from email_templates where description = 'Customers' and deleted = 0;');

change it to

    $result = mysql_query(
              "SELECT id FROM 
                  email_templates 
                WHERE 
                  description = 'Customers' 
                AND deleted = 0"
             );

To list the result

$options = '';
while($row = mysql_fetch_assoc($result)) {
       $options .= '<option value="'.$row['id'].'">'.$row['id'].'</option>' . "\n";   
}

//display the options list in html where you want
<select id="email_template0">
   <?php echo $options; ?>
</select>

now from jQuery handle event on dropdown change

 $(function() {
 $("#email_template0").change(function() {
    alert( $('option:selected', this).val() );
    //do your hide and select here 
    });
 });

Upvotes: 0

cyberrspiritt
cyberrspiritt

Reputation: 946

Use a hidden form element, set its value to the result obtained from mysql. Assign a specific ID to that form element. From jQuery, refer that form element using the ID. That should do the trick.

Upvotes: 1

Related Questions