0

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;');
...............
?>
2
  • in what you are going to show or use the sql return value?? Commented Nov 19, 2014 at 5:58
  • description = 'Customers' should be description = "Customers" (with double quotes) since you are using the single quotes as surrounding quotes for query. Commented Nov 19, 2014 at 6:31

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I know. I don't know how to get the MySQL output into the: $("#email_template0 option[value='value from MySQL']").hide();
Try using value='<?php echo $result ?>'
0

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 
    });
 });

4 Comments

Great, thanx, that's the php part. What would be the jquery for the same? Keeping in mind there are 2 queries for Contacts and Leads.
I'm still not able to get it. This is the php <?php $con = mysql_connect('mysql', 'admin', 'admin'); mysql_select_db('suitecrm'); $data_parent_type0 = mysql_real_escape_string($_POST['data_parent_type0']); $result = mysql_query('SELECT id FROM email_templates WHERE description = "'.$data_parent_type0.'" AND deleted = 0'); $options = ''; while($row = mysql_fetch_assoc($result)) { $options .= ''.$row['id'].'' . "\n"; echo $options; } mysql_close($con); ?>
And this is the jquery $(document).bind('DOMNodeInserted', '#composeHeaderTable0', function(event) { $("#data_parent_type0").change(function() { var data_parent_type0 = $('#data_parent_type0').val(); $.post("etplate_dbcall.php", { data_parent_type0: data_parent_type0},'html'); }); $("#email_template0 option[value='$options']").show(); });
The PHP returns the correct 'id', but the jqyuery is not re-posting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.