I have a small problem. I need to send a php variable from php file to second php file using setInterval. I dont know how i can write php variable in jquery code.
first.php
<?php
$phpvariable=1;
?>
In JavaScript
setInterval(function odswiez(id)
{
$('#chat').load('second.php?id=<here php variable how?>');
}, 3000);
});
second.php
<?php
$w=$_GET['id'];
echo $w;
?>
you need to echo your value
$('#chat').load('second.php?id=<?php echo $variable ?>');
}, 3000);
Alternative to do this will be..
$('#chat').load('second.php?id=<?php echo "id-{$variable}" ?>');
}, 3000);
insted of
setInterval(function odswiez(id)
{
$('#chat').load('second.php?id=<here php variable how?>');
}, 3000);
to
setInterval(function odswiez(id)
{
$.ajax({
url: "second.php",
type: "GET",
data: { id: "<?php echo $php_id; ?>" }
success: function(data){
$('#chat').load(data);
},
error: function(){}
});
}, 3000);
I think that's isn't the best way to manage this kind of Ajax calls,
First, I suggest you instead of bringing an entire PHP file passing a GET argument, you should just pass the params and receive just plain data.
var myvalue = $('.whateverinput').val();
$.ajax({
type: "POST",
dataType: "json", //can be post or get
url: "second.php", //you php who will recive data
data: {myname : myvalue},
success: function(data) {
// Callback if all things goes Ok you will recive data
$(".the-return").html("here we atach the response"+data);
},
error: function(xhr, error){
//if something wrong callback do something
console.debug(xhr); console.debug(error);
}
});
I recommend using JSON to send/receive your data between PHP and javascript.
You can find more info in :
Example using ajax php and using json
Good Luck!
Related
I would like to get the value from ajax with post in php, but this is not working, here is my code:
<script type="text/javascript">
function deletarPerson(id_person) {
let pag = "<?php echo plugin_dir_url(__DIR__) . 'admin/'; ?>";
jQuery.ajax({
url: pag + "/config.php",
method: "post",
data: {
id_person: id_person
},
dataType: "html",
success: function(result) {
<?php
$aux = $_POST['id_person'];
?>
alert('<?php echo $aux ?>')
},
})
}
</script>
Someone can help me?
Your Javascript ajax code:
function deletarPerson(id_person) {
jQuery.ajax({
url: "/echo.php", // <-- sending to echo.php on the server
// ...use whatever PHP file, on the server,
// you need to get the necessary information
method: "post",
data: {
id_person: id_person
},
dataType: "text",
success: function(result) {
alert(result) // <-- now use Javascript and the result variable,
// which is text sent back from the server,
// and do what you need to with it on the client
},
})
}
...and your PHP file:
<!-- echo.php -->
<?php
$aux = $_POST['id_person'];
echo $aux; // <-- whatever is echo'ed or print'ed in this file
// gets sent back to the server as text
// and is loaded in the Javascript result variable
// OR you can actually get some useful information from the server
// and send that back instead
?>
Read more about the distinct executions contexts between PHP on the server and Javascript/HTML/CSS... on the client.
I am new to Ajax. I have here a function that when a button is clicked, you should be redirected to id.php and at the same time pass the value of clicked_id.
Javascript code:
function clicked(clicked_id){
window.alert("clicked");
window.alert(clicked_id);
$.post('id.php',{ID:clicked_id},
function(data){
window.alert("here");
window.location='id.php';
});
}
Inside my id.php,
<?php
$clickedID = $_GET['ID'];
echo 'here at id.php';
echo $clickedID;
?>
Now the problem is that ID in id.php cannot be identified.
Please help me. I already tried both $_POST and $_GET.
I believe that the problem here is in the passing of the variable.
there is no need for ajax if you wanna pass the id to the id.php after redirection
function clicked(clicked_id){
window.alert("clicked");
window.alert(clicked_id);
window.location='id.php?id=' + clicked_id;
}
in id.php you can get the id like this:
<?php $id = $_GET['id'];
function buttonClicked() {
$.ajax({
url: "id.php", //url for php function
type: "POST",
data: {'clicked_id':clicked_id}, // data to sent
dataType: 'json',
success: function (data)
{
}
});
}
And in your id.php file:
$_REQUEST['clicked_id']
I , im working with Ajax and Codeigniter to call function client-server
the php
public function mainViewClean() {
unset($_SESSION[$this::jsondevices]);
unset($_SESSION[$this::jsontags]);
return "Ready";
}
//route $route['cleantags'] = 'user/mainViewClean';
And ajax:
<script type="text/javascript">
$(document).ready(function(){
$("#btn_recargar").button().click(function(){
//window.location.href = "<?= base_url('home')?>";
$.ajax({
type:'POST',
url:'<?php echo base_url("cleantags"); ?>',
data:{'id':100},
success:function(data){
//window.location.href = "<?= base_url('home')?>";
alert(data);
}
});
});
});
</script>
The function excuse good , but javascript don't show any data , what im doing wrong?
Well, the ajax call reads the response from the server, and that response must be rendered as some type of readable data, such as application/json or text/html.
In order to write that data, you need to echo it from the server with PHP.
The return statement doesn't write data, it simply returns at the server level.
If you want to communicate between PHP functions, you have to use return. But if you want to output some data, you have to use echo
Client side
$.ajax({
url:'<?php echo base_url("cleantags"); ?>',
dataType: 'application/json',
success:function(response)
{
alert(response.foo);
}
})
Server Side
public function mainViewClean()
{
unset($_SESSION[$this::jsondevices]);
unset($_SESSION[$this::jsontags]);
echo json_encode( array("foo"=>"Ready"));
}
Change return into :
echo "Ready";
If you're sending an array, at server side you need to json_encode, example :
// encode array into json string format
echo json_encode( array( 'name' => 'Osman' ) );
And in Js, you have 2 options, the 1st solution is :
success : function ( data ) {
// data now is coming in this form { "name" : "osman" }
// as the string data is coming from server-side
// you must parse it back into Javascript object
var newData = JSON.parse( data );
}
And the 2nd option is, add dataType properties inside ajax properties like following :
$.ajax({
...
dataType : 'json', // with this, no need to write JSON.parse()
...
});
I'm fairly new as I only have been using AJAX , but I think your code has a few syntactical errors.
data: { id:100 } with no quotations around id.
I advise you need to look at more examples of ajax calls to fix these little errors.
You said your JS is working but not showing data?
I want passing 2 parameters to PHP page via AJAX and load the response, but this code is not working.
JavaScript:
$(".show_category").click(function(){
var category_id = $(this).attr('data-category');
$.ajax({
url: "conx.php",
method: "POST",
data: {
action: "sort_category",
category_id: category_id
},
success: function(data) {
$("#con").load("conx.php");
}
});
});
PHP:
<?php
echo "1".$_POST["action"]."<br/>";
?>
You issue is here:
success: function(data) {
$("#con").load("conx.php");
}
At this point, you have already posted the data and received the HTML response. There is no need to make another HTTML request by calling .load, and doing so will mean requesting it again without the POST data, so it will not have the intended effect. Just use the HTML you already have in the data argument.
success: function(data) {
$("#con").html(data);
}
On a side note, this PHP code is a reflected XSS vulnerability:
<?php
echo "1".$_POST["action"]."<br/>";
?>
I posted a more specific question on this yesterday, but I think my problem is more basic than what I initially asked.
I am trying to use PHP to set a setTimeout() with a wait variable from a database, but any script I echo doesn't work, even if it involves no PHP manipulation. Take a look below.
Here is the ajax call
function loadContent()
{
$.ajax(
{
url: "controller/loadContent.php",
cache: false,
success: function(html)
{
$("#contentWindow").html(html);
}
});
}
// Initial Load
loadContent();
And here is the PHP it receives.
echo '<h1>Upload content to start the show</h1>';
echo '<script>
setTimeout(loadContent, 4000);
</script>';
The is showing, so I believe the ajax and the PHP is working properly. The script works properly when I place it inside the javascript file, but the script won't run when it's echoed to the page.
My problem is the javascript I echo doesn't run. How can I use PHP to send javascript to the user? Why is what I wrote not functioning?
UPDATE: I realized that when I echo script, it echoed to the middle of the body and technically is above where the script file is loaded on the bottom of the body. Can I echo to the bottom of the body?
Here is a workaround:
function loadContent()
{
$.ajax(
{
url: "controller/loadContent.php",
cache: false,
success: function(html)
{
var myArray = html.split("|");
var message = myArray[0];
var counter = parseInt(myArray[1]); //Minor fix
$("#contentWindow").html(message);
startCounter(counter);
}
});
}
function startCounter(counter){
//counter as the value of 1000 milliseconds are equal to 1 second
setTimeout(loadContent, counter);
}
PHP File
$refreshTimer = 1000; //This is a test
echo "Upload content to start the show|".$refreshTimer; //The message and the counter
If you return just the script portion in your php file you can set the dataType in the .ajax call to 'script' and it will execute the returned javascript (http://api.jquery.com/jquery.ajax/)
So:
function loadContent()
{
$.ajax(
{
url: "controller/loadContent.php",
cache: false,
dataType: 'script',
success: function(html)
{
$("#contentWindow").html(html);
}
});
}
// Initial Load
loadContent();
Then in your PHP:
echo 'setTimeout(loadContent, 4000);';