0

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 different way to write this? I have DW cs5.5 I can't upgrade Dreamweaver version.

    if ( $('#postage6').val() == "Your Permit Standard" ) {
        $('#postage6rate').val('<?php echo $your_permit_standard; ?>');
    }

Putting a backslash before the question mark just makes it print like this, which is not right.

    if ( $('#postage6').val() == "Your Permit Standard" ) {
        $('#postage6rate').val('<\?php echo $your_permit_standard; ?>');
    }

when it renders, there is supposed to be a value like this:

    if ( $('#postage6').val() == "Your Permit Standard" ) {
        $('#postage6rate').val('0.333');
    }

Also this doesn't work:

    if ( $('#postage6').val() == "Your Permit Standard" ) {
        var somevar = "<?php echo $your_permit_standard; ?>";
        $('#postage6rate').val(somevar);
    }

The syntax error just transfers from the line where the PHP variable was to the new line where the PHP variable is.

2
  • well, first of all ... php is server side code and javascript client side... the way you are doing it is not incorrect... however,it doesn't follows the standard... there are some other work around as in setting the values in server side... but then if you somehow need server values in javascript then this is the only way..or using ajax() which i guess in your code is unneccessary.. Commented Nov 11, 2013 at 17:43
  • Is your code in-line (inside *.php) file, otherwise it wont work! Commented Nov 11, 2013 at 17:45

3 Answers 3

7

You could define the value in a separate php block:

<script type="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...

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

2 Comments

at first i wasn't getting this to work, but then i figured out my mistake. this seems to do the trick. thanks.
I am facing the same problem can you please tell me that what is your "mistake"?
1

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?

<input type="text" name="postage6rate" value="<?php echo $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.

<input type="text" name="postage6rate" data-permit="<?php echo $your_permit_standard; ?>">

And then when you need to run your script…

window.addEventListener('onSomeEvent', function addTheData() {
  var $input = $('input[name="postage6rate"]');
  $input.val($input.data('permit'));
});

4 Comments

I think your didn't see his if statement
Ok, but I don’t think that was worthy of a downvote.
I didn't vote down! Your syntax is right but far from what he wants to achieve!
i would do it that way, but it's a large complicated app where i'm saving not just the name of a particular field but a number value of the field too so i'm inserting the name one way into a field then inserting the number value into the other field a different way, and on top of that there are many different calculations going on at the same time using same fields so i can't have that php variable in the value attribute like that.
0

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>
    <input id="postage6" name="postage6" value="Your Permit Standard"/>
    <input id="postage6rate" name="postage6rate"/>
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script>
    if ( $('#postage6').val() == "Your Permit Standard" ) {
        $('#postage6rate').val('<?php echo $your_permit_standard; ?>');
    }
    </script>
  </body>
<html>

And here's the working example

Comments

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.