I'm having problems with passing variables from jQuery into PHP. I search for the solutions on the internet, and i came to the AJAX. Never used Ajax before, so i guess that the problem is known.
So i have the following code in "index.php"
$("#inviteForm").submit(function(e) {
e.preventDefault();
var emailVal = $("#email").val();
$.ajax({
type: "POST",
url: "processAjax.php",
data: {email: emailVal},
success: function(data) {
alert(data);
}
});
});
In form, i have one input box (for email) and a submit button (the method is POST).
In processAjax.php i have the following code
<?php
$x = $_POST['email'];
return $x;
?>
So if i'm correct, if the $.ajax function is OK, the alert box should pop up. But it doesn't.
i've also tried alert(x); but it didn't work.
Any idea what i'm doing wrong
Try echo $x; instead of return $x;
Try this:
<?php
$x = $_POST['email'];
echo $x;
?>
Try this for better data manipulation. Use json_encode from the server side and json datatype to your ajax calls. Then to alert server response, just alert the key of the array like alert(data.value):
$.ajax({
type: "POST",
url: "processAjax.php",
data: {email: emailVal},
dataType: 'json'
success: function(data) {
alert(data.value);
}
processAjax.php
$result['value'] = $_POST['email'];
echo json_encode($result);
Related
I am attempting to update a database using ajax and PHP. Right now, i am just testing to see if my receiving PHP file is getting the POST data by echoing it, but all I get back is an 'undefined index' error.
Here's my sending code:
var content = $(".homeContent").html();
var dataString = "homepage|content|"+content;
//var
$.ajax(
{
type: "POST",
url: "Update.php",
data: "dataString="+dataString,
success: function (result) {
console.log(dataString);
}
});
and here is my receiving file (Update.php):
<?php
echo $_REQUEST['dataString'];
?>
Try this:
Ajax (JavaScript):
$.post( "Update.php", { dataString: "some String" } );
PHP:
<?php
echo $_POST['dataString'];
?>
Apologies in advance for any mistakes. This is my first post on StackOverflow.
So basically I have a php function. Inside it is a jQuery click function with a variable, into which I'm saving the clicked element's text. I'm trying to send that variable's value to my Uredi.php file, via $.ajax function, to save it into a php variable. However, I'm getting an "Undefined index" error. Help would be greatly appreciated. Thank you.
Here's the code:
PHP function:
public function uredi()
{
?>
<script>
$('.edit').click(function(){
$('.naslov_edit').val($(this).text());
$('#on_top').hide();
var refId = $(this).text();
$.ajax({
url: 'uredi.php',
type: "POST",
data: {id: refId},
success: function(data){
alert(data);
}
});
});
</script>
<?php
}
Uredi.php:
<?php
include 'Funkcije.php';
$uredi = new urejanje();
$uredi->uredi();
$id = $_POST['id'];
echo $id;
?>
Funkcije.php is the file with the uredi() function.
EDIT: Thank you everyone for helping. I figured out the problem. Turns out I just had to clean up my code a bit.
Why are you assigning $(this).text() to var id and then not using it in the $.ajax()? Remember scope and what doe $(this) actually reference within the $ajax()? I would also shy away from naming variables "id" Try:
var refId = $(this).text();
$.ajax({
url: 'uredi.php',
type: "POST",
//dataType: 'json',
data: {id: refId},
success: function(data){
alert(data);
}
});
Use this;
$.ajax({
url: 'uredi.php',
type: "POST",
data: "id=" + refId,
success: function(data){
alert(data);
}
});
perhaps you meant to read the post vars before initializing the class and maybe even pass Id in?
$id = $_POST['id'];
$uredi = new urejanje();
$uredi->uredi($id);
In any case I think You'll need to show us whats inside Funkcije.php
I have a AJAX script which sends value to PHP script and to retrieve the value from the PHP script. The part where the script sends the value is working fine. Its the problem with the retrieving values. I am not able to figure out what is wrong.
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: data,
dataType:'json',
success:function(data1) {
var y1=data1;
console.log(data1);
}
});
}
});
});
});
PHP code:
<?php
$userAnswer = $_POST['name'];
echo json_encode($userAnswer);
?>
Please check whether "name" is posted before assigning the value to $userAnswer.
Both ajax scripts are sending to "ajax.php". In first ajax request "name" is posted, but in 2nd ajax request "name" is not posted.
To see warnings and errors, enable error reporting in php.
<?php
//To enable error reporting
ini_set('display_errors',true);
error_reporting(E_ALL);
data: {name: 145}
try this hope this will work.
Your nested AJAX call does not have the request type specified. The default is GET but your ajax.php is trying to find a POST.
$(document).ready(function() {
$("#raaagh").click(function() {
$.ajax({
url: 'ajax.php',
type: "POST",
data: ({name: 145}),
success: function(data) {
console.log(data);
$.ajax({
url:'ajax.php',
type: "POST", //<-- added here
data: {name:data}, //<-- also required for POST
dataType:'json',
success:function(data1) {
var y1=data1;
console.log(data1);
}
});
}
});
});
});
Set type:'POST' inside the second ajax call and try to use data1[0]. Also remember that you are sending a json string (that comes from the first ajax) with the second request. Basically you are encoding an encoded value,so when you receive the post value you should json_decode the post value
I am trying to get the hang of php and ajax post. I am posting simple user input via text box to a php page. The php page takes the user input, counts words and sends it back as response. However it can't receive the data, comes back saying. Why is this how do I fix it?
This is my jQuery ajax post code
var dataString = name;
$.ajax({
type: "POST",
url: "test_get.php",
data: dataString,
success: function(response) {
alert(response);
}
});
return false;
});
});
This is my PHP code
// if data are received via POST, with index of 'dataString'
if (isset($_POST['dataString'])) {
$str = $_POST['dataString']; // get data
echo "The string: '<i>".$str."</i>' contains ". strlen($str). ' characters and '. str_word_count($str, 0). ' words.';
}
else echo 'There is no data!';
Your data argument is incorrect.
var dataString = name;
$.ajax({
type: "POST",
url: "test_get.php",
data: {"dataString" : dataString },
success: function(response) {
alert(response);
}
});
Read the jQuery $.ajax docs for more info.
You probably need to change:
data: dataString,
To
data: "dataString="+dataString,
Because a HTTP POST still runs off of key/value pairs.
I got very strange bug on IE. I used the code below to make a ajax request to get data from database and create drop down option <option> with that data. The thing is on IE the data didn't appear properly, it's only showing first character of data, but on other browser the data are showing correctly. I tried printing out the data as well, data are correct.
So I suspected either of my Jquery selection or append is wrong and tried appending outside the ajax call with some junk data and data are showing correctly but then again I put that code inside the ajax and its not showing again... I really out of clue now.. Can you guy plz help me with this? I have tested with FF, Chrome and safari they all working fine...
$.ajax({
cache: false,
type: "POST",
url: "<?php echo $this->baseUrl(); ?>/rewards/getdiscounttype/",
data: { esid: estore_id }
}).done(function( data ) {
var obj = jQuery.parseJSON(data);
jQuery.each(obj, function (i, app) {
//alert(i+app['discount_type']);
var sel = $('select[name=discount_type'+id+']');
sel.append('<option value="'+app['id']+'">'+app['discount_type']+'</option>');
});
});
$.ajax({
cache: false,
type: "POST",
url: "<?php echo $this->baseUrl(); ?>/rewards/getdiscounttype/",
data: { esid: estore_id },
datatype: "json",
succes: function(jsonData){
$.each(jsonData,function(i,app){
var sel = $('select[name=discount_type'+i+']');
sel.append('<option value="'+app.id.+'">'+app.discount_type.+'</option>');
});
},
error: function(e){
}
})
Try this, and I think its better to treat JSON as objects like app.id in stead of app['id']. Haven't had much time to do some testing, Ive you would give me the json you receive I can test.
Working now with JavaScript!
$.ajax({
cache: false,
type: "POST",
url: "<?php echo $this->baseUrl(); ?>/rewards/getdiscounttype/",
data: { esid: estore_id }
}).done(function( data ) {
var obj = jQuery.parseJSON(data);
/* clearing options with JS since jQuery cleare the select in the DOM but enter code herenot on screen. */
$('select[name=discount_type'+id+']')[0].options.length = 0;
$('select[name=discount_type'+id+']').children().remove().end().append('');
jQuery.each(obj, function (i, app) {
var sel = $('select[name=discount_type'+id+']');
sel.append('<option value="'+app.id+'">'+app.discount_type+'</option>');
});
});