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"); });
 ?> 
Tom
  • 537
  • 3
  • 7
  • 14
  • Why are you embedding JS into PHP!? – Jakub Oct 31 '12 at 19:42
  • to remain on the same page after form submit http://stackoverflow.com/questions/3849968/php-contact-form-want-to-stay-on-my-site-after-send – Tom Oct 31 '12 at 20:39

4 Answers4

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>";
?> 
DACrosby
  • 11,116
  • 3
  • 39
  • 51
  • thanks for the correct answer Douglas! I had tried echoing the jquery code before but left out the :" – Tom Oct 31 '12 at 20:48
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"); });';
  ?>
Jay Rizzi
  • 4,196
  • 5
  • 42
  • 71
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.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
  • 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 http://blog.themeforest.net/resources/html-css-php-and-jquery-killer-tutorials/ . – Kyros Oct 31 '12 at 19:44
  • 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. – Levi Botelho Oct 31 '12 at 19:52
  • ...and W3 Schools is evil. www.w3fools.com. – Levi Botelho Oct 31 '12 at 19:53
0
 // turn off php
?>   
$("#form1").submit(function() { 
    $.post("customBook-index.php");
    return false; 
    alert ("submit form 1"); 
});
<?php 
NappingRabbit
  • 1,888
  • 1
  • 13
  • 18