0

having trouble calling a Jquery function from within PHP. I'm getting a syntax error when I'm using the below code:

if(!empty($_POST['selectAGE'])){
 $username = $_POST['selectAGE'];
}
if(empty($_POST['selectAGE'])){
 echo '<script type="text/javascript">'
 , '$("#selectAGE").prepend("<option value=''></option>").val('');'
 , '</script>';
}

Am I not escaping properly or something?

further to this, I'm then checking this select box by using

if ( form.selectAGE.selectedIndex == 0 ) { alert ( "Enter the correct Age." ); return false; }

however, once the form has been submitted, it eliminates the prepended 'empty' value and now the one you've selected is selectedindex 0. so the above check returns that you haven't entered it. How can I amend the line above to take this into account?

asked Jun 11, 2012 at 17:42
1
  • 2
    Just a clarification: you are not "calling a jQuery function from within PHP". That would be impossible. What you're doing is echoing javascript/jquery code from PHP. That code will be run on the client (browser), whereas PHP runs on the server. Commented Jun 11, 2012 at 17:46

3 Answers 3

3

Escape the characters here:

'$("#selectAGE").prepend("<option value=\'\'></option>").val(\'\');'
answered Jun 11, 2012 at 17:43
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome! works a treat, thanks so much, you certainly speeded up the end of my already 13 hour shift :)
@ChrisSpalton - No problem we all have days like that. Don't forget to mark as answer :-)
further to this, I'm then checking this select box by using if ( form.selectAGE.selectedIndex == 0 ) { alert ( "Enter the correct Age." ); return false; } however, once the form has been submitted, it eliminates the prepended 'empty' value and now the one you've selected is selectedindex 0. so the above check returns that you haven't entered it. How can I amend the line above to take this into account?
1

If the escaping is giving you trouble I would just write the JS, instead of echoing it out.

if(empty($_POST['selectAGE'])): ?>
 <script type="text/javascript">
 $('#selectAGE').prepend('<option value=""></option>').val('');
 </script>
<?php endif;

I prefer using the endif instead of matching brackets between JS and PHP.

answered Jun 11, 2012 at 17:47

Comments

0

You need to escape the single quotes. Try this:

<?
if(!empty($_POST['selectAGE'])){
 $username = $_POST['selectAGE'];
}
if(empty($_POST['selectAGE'])){
 echo '<script type="text/javascript">',
 echo '$("#selectAGE").prepend("<option value=\'\'></option>").val(\'\');'
 , '</script>';
}
?>
answered Jun 11, 2012 at 17:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.