I have the following ajax code:
<script type='text/javascript'>
$(document).ready(function () {
$('.questionform').on('submit', function(e) {
e.preventDefault();
$.ajax({
url : "aplaygroundajaxtest.php",
type: "POST",
data: $(this).serialize(),
success: function (data) {
'<?php
if($data3["{$QuestionType}Percent"]<100){ ?>' alert("Alert Message OnClick"); '<?php ;} ?>'
},
});
});
});
</script>
If the value in column {$QuestionType}Percent
is less than 100, I want it to alert a message. However, it does not work. the source code shows the following:
<script type='text/javascript'>
$(document).ready(function () {
$('.questionform').on('submit', function(e) {
e.preventDefault();
$.ajax({
url : "aplaygroundajaxtest.php",
type: "POST",
data: $(this).serialize(),
success: function (data) {
'' alert("Alert Message OnClick"); ''
},
});
});
});
</script>
I've tried escaping the "
with \
and I've also tried double quotes around the php (in which case, the PHP still disappears in the source code, leaving empty double quotes).
I know that the issue does not lie with $data3["{$QuestionType}Percent"]<100
as it functions correctly elsewhere in my code - and the value in the column is indeed less than 100.
EDIT / UPDATE ---------------------------------------------->
The issue was two-fold: 1) I needed to get rid of the single quotes around PHP
tags; 2) it wasn't receiving the value of $data3["{$QuestionType}Percent correctly.
In the URL to which the form posts (aplaygroundajaxtest.php), I corrected the latter issue; now, I'm using the variable $QuestionTypePercent. In the code below, print_r($QuestionTypePercent);
shows 14
in the source and the if
condition is correctly interpreted. The source shows 14 alert("Alert Message OnClick");
and I get the error Uncaught SyntaxError: Unexpected identifier
. When I remove print_r($QuestionTypePercent);
the error goes away and the code works. Why is print_r($QuestionTypePercent);
interfering with my alert in the code below?
<script type='text/javascript'>
$(document).ready(function () {
$('.questionform').on('submit', function(e) {
e.preventDefault();
$.ajax({
url : "aplaygroundajaxtest.php",
type: "POST",
data: $(this).serialize(),
success: function (data) {
<?php
print_r($QuestionTypePercent); // RETURNS 14 CURRENTLY
if($QuestionTypePercent < 100){?>
alert("Alert Message OnClick"); //REDIRECTSME TO aplaygroundajaxtest.php
<?php } ?>
},
});
});
});
</script>