How Do I Insert Php Variable Inside Jquery/javascript Properly?
This code works when running the application, but Dreamweaver is giving a syntax error. It doesn't like the question mark there. I like DW to be syntax error free. Is there a di
Solution 1:
You could define the value in a separate php block:
<scripttype="text/javascript">var value = '<?=$your_permit_standard?>';
</script>
And then use it in your JS:
if ( $('#postage6').val() == "Your Permit Standard" ) {
$('#postage6rate').val(value);
}
But then you would be introducing JS dependency in PHP, which I wouldn't recommend, but since you're mixing both anyway...
Solution 2:
Looks like you’re setting an input value with JavaScript, after having set the .val()
method argument with PHP. Why not set the value of the input with PHP directly?
<inputtype="text"name="postage6rate"value="<?phpecho$your_permit_standard; ?>">
If you need to run this script at some time other than page load, you could bind the data to an element with the data
attribute.
<inputtype="text"name="postage6rate"data-permit="<?phpecho$your_permit_standard; ?>">
And then when you need to run your script…
window.addEventListener('onSomeEvent', functionaddTheData() {
var$input = $('input[name="postage6rate"]');
$input.val($input.data('permit'));
});
Solution 3:
I assume that your javascript
isn't in-line (in the same PHP
file) which prevents PHP
from executing
Try:
<?php$your_permit_standard = "0.335";
?><html><body><inputid="postage6"name="postage6"value="Your Permit Standard"/><inputid="postage6rate"name="postage6rate"/><scriptsrc="http://code.jquery.com/jquery-2.0.3.min.js"></script><script>if ( $('#postage6').val() == "Your Permit Standard" ) {
$('#postage6rate').val('<?phpecho$your_permit_standard; ?>');
}
</script></body><html>
And here's the working example
Post a Comment for "How Do I Insert Php Variable Inside Jquery/javascript Properly?"