Call AJAX / jquery in the middle of PHP code

Asked

Viewed 327 times

-4

I have a PHP code structure and, within it, I want to automatically call a $.get AJAX for another PHP page.

Example:

<?php $cod = 1; $mod = 2; $text = 3; 
como chamar um ajax automaticamente aqui, sem ter que clicar em botão algum, conforme exemplo abaixo:
echo "<script type='text/javascript'>
    $.get('../print.php?cod =$cod&mod=$mod&text=text');
</script>"
//continuo com o código php
$bla = 1; $bla2 = 2; etc..... `

I mean. in the middle of mine script PHP I want to call via AJAX another script you will send another routine and continue in my mother page routine.

I don’t know if I was clear, but I need help.

  • 4

    You won’t be able to do that. PHP runs on the server and Javascript on the client. In a way this might work, but the AJAX request would only occur when the browser ran Javascript. If you need to make an HTTP request with PHP, use the Curl library.

  • Dude, you’re a genius. I got so focused on ajax that I completely forgot I could call by Curl since I was already in php. Muuuuittoo :)

  • Hello Dvd. As a study, can you simulate an example? Thank you :)

  • 4

    @DVD Use AJAX inside a echo is not the same as making an AJAX request in PHP. What is printed by echo will be part of the body of the HTTP response that the server will send and will only be processed in the client. If the request needs to be made on the server side, this will not solve the problem, as I said.

  • Place this code on a PHP page: <?php echo "<script type='text/javascript'>alert('olá');</script>";?> and look at the result... the same way you can give a alert in JS, you can call an Ajax

  • @Andersoncarloswoss got it now. He wants to call a PHP Ajax inside a JS script. It was bad. :)

  • No, you just want to make an HTTP request for another PHP file. There’s no reason to have AJAX/JS here.

  • Exactly. I got so focused on a possible solution that I didn’t think about the simple one. By posting here, you have looked from an external perspective and already opened my mind. Curl solved my situation immediately. Thank you all.

  • If you are PHP with PHP you may not even need Curl. Often a include/require resolve, or even scheduled run by crontab or task scheduler.

  • 1

    Novatodaweb, if possible, answer your question by asking how you solved the problem. As Bacco said, you may not even need to make another request.

  • Imagine the scenery. I’m in the middle of a series of processes (a, b, c, d, e, f, g, h, i .) such as an impression. What I did, was when this reply comes back true, I make a call via Curl (replacing the attempt in ajax) to another script to run that process and then I finish the rest of the processing I was doing, without having to leave the script and return, understood?

Show 6 more comments

1 answer

0

The purpose of ajax is necessarily to make an asynchronous request, for cases where you rely on user data and/or upload information only when necessary, without having to update the page completely. And it is mandatory that to be an ajax this call is made by the front language, and is only executed after the page has already been set up by PHP.

I believe that what you wanted to do and only include more code to process on the server, just give a include of the PHP file you want to run.

Remember that unlike Javascript PHP is a synchronous language and will only run the next command when the previous one is not possible to do a php ajax for php.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.