JS in (start.php)
$(document).ready(function()
{
$('#btn_1').click(function(){
$.ajax({
type: "POST",
url: "get_data.php",
data: 'func=getData1',
success: function(msg){
$('#div_1').html(msg);
}
});
$('#div_1').show();
})
});
PHP (somename.php)
<?php
session_start();
if(trim($_POST['func']) == "getData1")
{
echo "Test";
}
?>
How can i pass the sessionid from start.php through my ajax to the get_data.php file ?
And how can pass the complete URL "url: "get_data.php," to the js-File so that i can switch the php-files, that should be called from ajax ?
Store Session ID in javascript variable and send it through ajax call, like this:
var session_id = '<?php echo session_id();?>';
Complete code should be:
var data = {func:'getData1',session_id:session_id};
$('#btn_1').click(function(){
$.ajax({
type: "POST",
url: "get_data.php",
data: data,
success: function(msg){
$('#div_1').html(msg);
}
});
$('#div_1').show();
})
Updates
If you want to access php variable in external js file, define variable before including js file. Like:
<script type="text/javascript">
var session_id = '<?php echo session_id();?>';
</script>
<script src="./ajax.js" type="text/javascript"></script>
$(document).ready(function()
{
$('#btn_1').click(function(){
$.ajax({
type: "POST",
url: "get_data.php",
data: {func:'fuc_name',session:'<?php echo session_id();?>'},
success: function(msg){
$('#div_1').html(msg);
}
});
$('#div_1').show();
})
});
var session_id = '<?php echo session_id();?>';
$('#btn_1').click(function(){
$.ajax({
type: "POST",
url: "get_data.php",
data: {func:"getData1","session":session_id},
success: function(msg){
$('#div_1').html(msg);
}
});
$('#div_1').show();
})
});
Use json encode. By using json you can pass the php data to js. Changing the code as per below. Set session id after session starts.
$(document).ready(function()`{
$('#btn_1').click(function(){ ` $.ajax({ `type: "POST",` dataType:"json", `url: "get_data.php,` data: 'func=getData1'`success: function(msg){ ` $('#div_1').html(msg.id); `}
});
$('#div_1').show();
})
});
in php side echo the variable with json encode.
echo json_encode($id); `
Use json encode. By using json you can pass the php data to js. Changing the code as per below. Set session id after session starts.
$(document).ready(function()
'{
$('#btn_1').click(function(){ `
$.ajax({
type: "POST",`
dataType:"json", `url: "get_data.php,`
data: {func:'enter the data you want to pass'},
success: function(msg){
$('#div_1').html(msg.id);
}
});
$('#div_1').show();
})
});
in php side echo the variable with json encode.
echo json_encode($id); `
Related
I'm trying to pass a jQuery variable (var datastring), that is a string, through ajax to the external drug_scripts.php file that echos out the value. I can get it to work if I set the data to a numerical value, however if I set it to the variable it returns NULL.
I've set data: ({dataSTring: dataString}) -- returns NULL.
Setting data: ({name: 123}) --- returns 123
AJAX:
var dataString = '<?php print $DF_NAME1; ?>';
$.ajax({
url: "/wp-content/themes/Avada-Child-Theme/assets/php/drug_scripts.php",
type:"POST",
dataType: 'json',
data: ({dataString: 125}),
success: function(data){
console.log(data);
}
});
PHP:
$userInput = $_POST['dataString'];
echo $userInput;
Again, the current state returns 123. If you were to set data to data: (dataString) it returns NULL.
I will assume $DF_NAME1 is defined as something like <?php $DF_NAME1 = 'name'; ?> (which you will get somewhere, like the database)
Then:
<script>var dataString = "<?=$DF_NAME1?>";</script>
<script>
$( document ).ready(function() {
var dataToSend = 'dataString=' + dataString;
$.ajax({
url: "/wp-content/themes/Avada-Child-Theme/assets/php/drug_scripts.php",
type:"POST",
dataType: 'json',
data: dataToSend,
success: function(data){
console.log(data);
}
});
});
</script>
The PHP file:
<?php
$userInput = $_POST['dataString'];
echo json_encode($userInput);
?>
You need both ... attribute name and attribute value
....
type:"POST",
dataType: 'json',
data: ({attribute_name:'attribute_value'}),
....
in your case
....
type:"POST",
dataType: 'json',
data: ({dataString : dataString }),
....
in $_POST['dataString'] you should obtain the content of the var dataString
Try this it works as I have rewritten the code for you.
In the script below, You can see that I just alert the dataString variable to see if it has any value. if the alert is empty, it means you are not passing any value to the datastring variable.
I have also tested it with variable data string set to NancyMooree which I commented
<html><head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//var dataString = 'NancyMoore';
var dataString = '<?php echo $DF_NAME1; ?>';
// check that datastring has value in it by alerting it
alert(dataString);
var datasend = "dataString ="+ dataString ;
$.ajax({
type:'POST',
url:'/wp-content/themes/Avada-Child-Theme/assets/php/drug_scripts.php',
data:datasend,
crossDomain: true,
cache:false,
success:function(data){
console.log(data);
$('#listresult').fadeIn('slow').prepend(data);
}
});
});
</script>
</head>
<body>
<div id="listresult"> </div>
</body>
</html>
php
<?php
$userInput = $_POST['dataString'];
echo $userInput;
?>
hi i want to send javascript data via ajax to the same page in wordpress. Cannot fetch the data in testajax.php page. its shows Undefined index: name. And how to send the data on same php page rather than testajax.php ?
<script>
jQuery.ajax({
type: "POST",
url: 'http://localhost/projects/xxx/wordpress/wp-content/themes/xxx/testajax.php',
data: {name:'foo'},
success: function(data)
{
alert('success');
}
});
var foo = 'somename';
jQuery.ajax({
url:'yourUrl',
type: "POST",
data: {'name':foo},
success: function(data)
{
alert('success');
}
});
php
<?php
if(isset($_POST['name'])){
//Do whatever you want
}else{
//Do whatever you want
}
?>
jQuery.ajax({
url: "./_FILE_" // ./index.php for example
type: "POST",
data: {name:'foo'},
success: function(data)
{
alert('success');
}
});
I have 3 values stored in 3 seperate DIV tags and i want it to pass via ajax to php file. I have working code and stuck in passing all values to php file. Any ideas hoe to do it.
This is my js code:
$('#button').click(function(){
var slider_value = $('#slider_value').text();
var slider1_value = $('#slider1_value').text();
var slider2_value = $('#slider2_value').text();
$.ajax({
url:'placeDetailSend.php',
type: 'POST',
data: 'slider_value='+slider_value,
success: function(data){
$('#test').html(data);
}
});
});
and this it my php file:
<?php
if (isset($_POST['slider_value'])||($_POST['slider1_value'])){
echo $slider_value = $_POST['slider_value'];
echo $slider1_value = $_POST['slider1_value'];
}?>
Values are seperated by & as in a URL:
data: 'slider_value='+slider_value+'&slider1_value='+slider1_value+'&slider2_value='+slider2_value,
jQuery Code:
$('#button').click(function(){
var slider_value = $('#slider_value').text();
var slider1_value = $('#slider1_value').text();
var slider2_value = $('#slider2_value').text();
$.ajax({
url:'placeDetailSend.php',
type: 'POST',
data: {var1: slider_value, var2: slider1_value,var3:slider2_value },
success: function(data){
$('#test').html(data);
}
});
});
Use This php code for fetching values
<?php
echo $_POST['var1'];
echo $_POST['var2'];
echo $_POST['var3'];
?>
$('#button').click(function(){
var slider_value = $('#slider_value').text();
var slider1_value = $('#slider1_value').text();
var slider2_value = $('#slider2_value').text();
$.ajax({
url:'placeDetailSend.php',
type: 'POST',
data: {slider: [slider_value, slider1_value, slider2_value]},
success: function(data){
$('#test').html(data);
}
});
});
And in php file,
<?php
if (isset($_POST['slider'])){
$slider_value = $_POST['slider'];
echo '<pre>' . print_r($slider_value) . '</pre>';
}
?>
Try,
data: 'slider_value='+slider_value+'&slider1_value='+slider1_value+'&slider2_value='+slider2_value;
OR
data:{slider_value:slider_value,slider1_value:slider1_value,slider2_value:slider2_value}
You will get more about jquery ajax here
You are only passing one div value in the datastring. Pass all the values like this:-
$('#button').click(function(){
var slider_value = $('#slider_value').text();
var slider1_value = $('#slider1_value').text();
var slider2_value = $('#slider2_value').text();
$.ajax({
url:'placeDetailSend.php',
type: 'POST',
data: 'slider_value='+slider_value + '&slider1_value='+slider1_value + '&slider2_value='+slider2_value,
success: function(data){
$('#test').html(data);
}
});
});
$.ajax({
url:'placeDetailSend.php',
type: 'POST',
data: {
'slider_value': slider_value,
'slider_value1': slider_value1,
'slider_value2': slider_value2
},
success: function(data){
$('#test').html(data);
}
});
});
Firstly, you could store your data differently (in an array) like so:
var slider_values = new Array();
silder_values.push($('#slider_value').text(),$('#slider_value2').text(),$('#slider_value3').text());
And then, you can simply pass this array as a data object to the ajax request like so:
$.ajax({
url:'placeDetailSend.php',
type: 'POST',
data: {'slider_values': slider_values},
success: function(data){
// Do whatever here
}
});
});
However, if you use this method, you must be sure to loop through the $_POST['slider_values'] in PHP as it is now an array not a string. This is pretty simple:
foreach($_POST['slider_values'] as $value){
// Write the current value
echo $value
}
I want to pass values to a PHP script so i am using AJAX to pass those, and in the same function I am using another AJAX to retrieve those values.
The problem is that the second AJAX is not retrieving any value from the PHP file. Why is this? How can I store the variable passed on to the PHP script so that the second AJAX can retrieve it?
My code is as follows:
AJAX CODE:
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
PHP CODE:
<?php
$userAnswer = $_POST['name'];
echo json_encode($userAnswer);
?>
Use dataType:"json" for json data
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
dataType:'json', // add json datatype to get json
data: ({name: 145}),
success: function(data){
console.log(data);
}
});
Read Docs http://api.jquery.com/jQuery.ajax/
Also in PHP
<?php
$userAnswer = $_POST['name'];
$sql="SELECT * FROM <tablename> where color='".$userAnswer."'" ;
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
// for first row only and suppose table having data
echo json_encode($row); // pass array in json_encode
?>
No need to use second ajax function, you can get it back on success inside a function, another issue here is you don't know when the first ajax call finished, then, even if you use SESSION you may not get it within second AJAX call.
SO, I recommend using one AJAX call and get the value with success.
example: in first ajax call
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data){
console.log(data);
alert(data);
//or if the data is JSON
var jdata = jQuery.parseJSON(data);
}
});
$(document).ready(function() {
$("#raaagh").click(function() {
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: 145}),
success: function(data) {
console.log(data);
$.ajax({
url:'ajax.php',
data: data,
dataType:'json',
success:function(data1) {
var y1=data1;
console.log(data1);
}
});
}
});
});
});
Use like this, first make a ajax call to get data, then your php function will return u the result which u wil get in data and pass that data to the new ajax call
In your PhP file there's going to be a variable called $_REQUEST and it contains an array with all the data send from Javascript to PhP using AJAX.
Try this: var_dump($_REQUEST); and check if you're receiving the values.
you have to pass values with the single quotes
$(document).ready(function() {
$("#raaagh").click(function(){
$.ajax({
url: 'ajax.php', //This is the current doc
type: "POST",
data: ({name: '145'}), //variables should be pass like this
success: function(data){
console.log(data);
}
});
$.ajax({
url:'ajax.php',
data:"",
dataType:'json',
success:function(data1){
var y1=data1;
console.log(data1);
}
});
});
});
try it it may work.......
HTML/jQuery:
Friends
<script type="text/javascript">
$(document).ready(function() {
$('a#friends').click(function() {
$.ajax({
type: "POST",
url: "data.php",
data: $('#friends').html(),
success: function(data) {
$('#questions').html(data);
},
dataType: "HTML"
});
});
});
</script>
data.php:
<?php
echo $_POST['#friends'];
?>
How do I return this POST value of an id in an anchor tag? The variable is being passed to PHP because I can alert it, but the problem is getting it back.
You need to specify the name of the value you are sending across in your AJAX request. Try this:
$.ajax({
type: "POST",
url: "data.php",
data: { 'friends': $('#friends').html() }, // Note the value is sent in an object with a key of 'friends'
success: function(data) {
$('#questions').html(data);
},
dataType: "HTML"
});
<?php
echo $_POST['friends']; // retrieve the 'friends' value
?>
How you are passing the data to PHP,
please use the following code,
Friends
<script type="text/javascript">
$(document).ready(function() {
$('a#friends').click(function() {
$.ajax({
type: "POST",
url: "data.php",
data: {'friends' : $('#friends').html()},
success: function(data) {
$('#questions').html(data);
},
dataType: "HTML"
});
});
</script>
<?php
echo $_POST['friends'];
?>
Your syntax is wrong for passing friends value to data.php
Try this
$(document).ready(function() {
$('a#friends').click(function() {
$.ajax({
type: "POST",
url: "data.php",
data: "friends="+$('#friends').html(),
success: function(data) {
$('#questions').html(data);
},
dataType: "HTML"
});
});
<?php
echo $_POST['friends'];
?>
First of all you can't send data to the ajax page in this way
data: $('#friends').html(),
A more suitable way would be
data : {'key1':'val1', 'key2':'val2'}
Then on the php page, you can retrieve these values in this fashion
$key1 = $_POST['key1']; // will contain 'val1'
$key2= $_POST['key2']; // will contain 'val2'
alternatively you can use
Friends
<script type="text/javascript">
$(document).ready(function() {
$('a#friends').click(function() {
$.post("data.php",{
friends: $("#friends").html()
},function(data){
$("#questions").html($.trim(data)); // trim to be sure
});
});
});
</script>
and in the php:
<?php
echo $_POST['friends'];
?>
Pass the data variable in data field. For more info see below example
$(document).ready(function() {
$('a#friends').click(function() {
alert("");
$.ajax({
type: "POST",
url: "data.php",
data: "#friends="+$('#friends').html(),
success: function(data) {
alert(data);
$('#questions').html(data);
},
dataType: "HTML"
});
});
});