I have an HTML checkbox and I'm trying to test it using the script I received as an ajax response. Here is my HTML:
<form class="form-vertical sms-settings-form">
<div class="form-group">
<div data-toggle="tooltip" title="SMS to the customer at the time of sale." class="form-group opt-transport">
<label for="sale-sms">Sale SMS</label>
<input type="checkbox" id="sale-sms" data-toggle="toggle" data-on="Send" data-off="Don't Sent">
</div>
<textarea data-toggle="tooltip" class="form-control" id="sale-sms-content"></textarea>
</div>
<button type="button" class="btn btn-success sms-settings-btn">Save Settings</button>
</form>
Here is my AJAX request.
$(document).ready(function(){
$.post("ajax-req-handler.php", {key: "load-saved-settings"}, function(data){ $(".exec-ajax-script").html(data); });
});
Here is my ajax-req-handler.php
$sql = "SELECT * FROM settings";
$result = $conn->query($sql);
if(num_rows($result)>0){
while($row=$result->fetch_assoc()){
$id = $row['id'];
$setting_name = $row['name'];
if($id == 1 && $setting_name == 'sale-sms') $sms_content = $row['value'];
if($id == 2 && $setting_name == 'send-sms') $send_sms = $row['value'];
}
} ?>
<script type="text/javascript">
$(document).ready(function(){
$(".sms-settings-form #sale-sms").prop(<?php echo ($send_sms == 1) ? "'checked', 'checked'" : ""; ?>);
$(".sms-settings-form textarea").val("<?php echo $sms_content; ?>");
});
</script> <?php
And here is the code that I get as AJAX resopnse
<script type="text/javascript">
$(document).ready(function(){
$(".sms-settings-form #sale-sms").prop('checked', 'checked');
$(".sms-settings-form textarea").val("Hello [name], Your mobile number is [mobile]. your total amount is Rs. [total] out of which you paid Rs. [paid] and your due amount is [due] for the bill number # [bill] on [date] ");
});
</script>
The answer seems to be Ok, but still, this does not allow me to set the value of my checkbox. I tried to copy the response and paste it into my first file (the file where the checkbox is checked and the ajax request is initiated). He works there. The problem is only ajax answer
source
share