1

I am trying to use some jQuery functions inside of my php page which I am using for a wordpress plugin. I have imported the jquery api using the below code however I'm not sure how to write the function.

 <?php
    echo "Custom Book Settings Page";
    echo '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>';

this produces syntax error

 <?php   
     $("#form1").submit(function() { $.post("customBook-index.php"); return false; alert ("submit form 1"); });
 ?> 
2
  • Why are you embedding JS into PHP!? Commented Oct 31, 2012 at 19:42
  • to remain on the same page after form submit stackoverflow.com/questions/3849968/… Commented Oct 31, 2012 at 20:39

4 Answers 4

3

Like the others have said, you can't use JavaScipt (or any of its libraries) inside PHP. You certainly can, however, use PHP to print out JavaScript which will be run at the appropriate time.

<?php echo "<script type='text/javascript'>
  $(document).ready(function(){   
     $('#form1').submit(function() {
        $.post('customBook-index.php');
        return false;
        alert ('submit form 1');
     });
  });
</script>";
?> 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the correct answer Douglas! I had tried echoing the jquery code before but left out the :"<script> tags. The example I was drawing from didn't have them stackoverflow.com/questions/3849968/…
1

why wouldnt you just have the syntax without the tags?

 $("#form1").submit(function() { $.post("customBook-index.php"); return false; alert ("submit form 1"); });

if you have to have php write the statement, you forgot the echo

 <?php   
 echo '$("#form1").submit(function() { $.post("customBook-index.php"); return false; alert ("submit form 1"); });';
  ?>

Comments

1

You cannot use jQuery like that within your PHP. JQuery is a JavaScript library. It is essentially code that is pre-written for you and abstracted in such a way that it makes it easy to use. When you call $('#myElementId) you are calling an abstraction of a JavaScript function (or set of functions).

Using jQuery within PHP won't work, because the PHP interpreter has no way to make sense of it. It would be like speaking giving instructions in Chinese to a (monoglot) Anglophone. Furthermore, there is a significant difference between PHP and JavaScript in as much as PHP is executed on a web server, and JavaScript is executed on a client's machine. This is an important concept to understand for any web programmer.

In short, you either need to write your JS function into a <script> tag on the page such that the navigator parses it as JavaScript, or determine the PHP equivalent for what you are trying to do.

3 Comments

TLDR: PHP happens on the server, jQuery (and javascript) happen on the client. The two can communicate, but they happen in different places. Look at blog.themeforest.net/resources/… .
Well of course they can communicate (I mean that is the whole point of AJAX). That doesn't mean you can put one in the other.
...and W3 Schools is evil. www.w3fools.com.
0
 // turn off php
?>   
$("#form1").submit(function() { 
    $.post("customBook-index.php");
    return false; 
    alert ("submit form 1"); 
});
<?php 

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.