0

I have an ajax function that post to an PHP file. Now since I'm using WordPress I can use the get_url function so I don't need to hard code the entire URL.

The WordPress function is an PHP so I'm trying to use PHP inside the ajax post. But it wont do the trick. Any ideas ? and is it possible ?

This is what I have.

$(document).ready(function(){

$('#submit').click(function(){

$.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(),  function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;

});

});

I have also tried the php echo inside quotes like this.

   $.post(' "<?php echo get_template_directory_uri(); ?>" /send.php' ....

ether way I get this path

http://mysite.com/%27%3C?php%20echo%20get_template_directory_uri();%20?%3E%27/send.php&email=&message=&name=&sent=1
4
  • 3
    Where are you using that code? In a JS file? Commented Apr 6, 2013 at 15:05
  • Unless this is placed in header.php (for example), it won't work. The WordPress template functions are only loaded in the template files. Apart from that, there's nothing wrong with the code (the first part above). Commented Apr 6, 2013 at 15:07
  • Yes, forgot too mention that. was thinking of creating a variable but since in a js file it didnt to the trick Commented Apr 6, 2013 at 15:07
  • what you should do is rename the extension to php and include it as javascript it would work totaly fine like that Commented Apr 6, 2013 at 15:13

1 Answer 1

3

Javascript to PHP = nope you cant embed javascript to php, only php can embed html and javascripts.

the better way to do it is to create a .php file and insert the javascript there...

Example: js.php

<?php
    function doSubmit() {
        ?>
        <script>
            $('#submit').click(function(){
                $.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(), function(response) {
                    $('#success').html(response);
                    //$('#success').hide('slow');
                    });
                return false;
            });
        </script>
        <?php
    }
?>

call the js.php using "include(js.php);" and call the functions inside another php

Inside your index.php

<?php
include('js.php');
?>
<html>
    <head><script><?php doSubmit();?></script></head>
    <body>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

Alternatively, just put doSubmit() inside your functions.php file and you don't need to include the file. +1 anyway.
by the way yeah @Kieran is right, you could also do it...but my concept is to wide QStarter's imagination about php coding :)
Thanks. going to try this in a moment :) thanks alot @ChowKiko and the rest for all help
you may CHECK my post if my ANSWER is correct so that guest could identify the correct answer...thanks

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.