-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.
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.
– Woss
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 :)
– NovatodaWeb
Hello Dvd. As a study, can you simulate an example? Thank you :)
– NovatodaWeb
@DVD Use AJAX inside a
echo
is not the same as making an AJAX request in PHP. What is printed byecho
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.– Woss
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 aalert
in JS, you can call an Ajax– Sam
@Andersoncarloswoss got it now. He wants to call a PHP Ajax inside a JS script. It was bad. :)
– Sam
No, you just want to make an HTTP request for another PHP file. There’s no reason to have AJAX/JS here.
– Woss
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.
– NovatodaWeb
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.
– Bacco
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.
– Woss
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?
– NovatodaWeb