Ajax variable not passing to php - php

I am looking to pass the value of cityField in ajax to $_POST['cityField']. Everything looks in order but the variable is not passing to php on nextpage.php
<script type="text/javascript">
$('#city').blur(function() {
var cityField=$('#city').val();
$.ajax({
type:"post",
url:"nextpage.php",
data:{'cityField':cityField },
dataType:"html",
success:function(data) {
alert("You typed:"+cityField);
}
});
});
</script>

Do not use 'cityField' as object. Because data contains paramaters in the format data{ object: value}.
Use this:
<script type="text/javascript">
$('#city').blur(function() {
var cityField=$('#city').val();
$.ajax({
type:"post",
url:"nextpage.php",
data:{myobj:cityField },
dataType:"html",
success:function(data) {
alert("You typed:"+cityField);
});
</script>
and then retrieve myobj in server side.

In your success function you'll get the data send by nextpage.php in the var named data. You are alerting the cityfield variable.
So change your success function to resemble something like this:
....
success: function (data) {
console.log('This is the answer of nextpage.php : '+ data;
}
...
Besides that, maybe it's a good idea to strip the data to an object outside of your $.ajax call:
var myPostData = {};
myPostData.cityField = $('#city').val();
....
data: myPostData,
....

it's simple.change to :
...
data:$('#city').serialize()
...
or use this fully :
<script type="text/javascript">
$('#city').blur(function() {
var $this=this;
$.ajax({
type:"post",
url:"nextpage.php",
data:$($this).serialize(),
dataType:"html",
success:function(data) {
alert("You typed:"+cityField);
}
});
});
</script>
for more see this : http://api.jquery.com/serialize/

Related

Can't figure out why PHP file doesn't get called by Ajax

this is my Code inside a html file:
<script>
$(document).ready(function (event)
{
$(document).on("click", ".interestbutton", function(e)
{
var docID = e.target.parentNode.parentNode.id;
$.ajax({
type: "POST",
url: "interest.php",
data: {docID },
success: function(data) {
console.log("done");
}
});
});
});
</script>
The "console.log" works but nothing inside my php file does. Any ideas?
Best regards
Change data: {docID } for data: {docID: docID} as it is an object.

Through URL pass parameter to php, but php doesn't get it, Why?

I create the table and use ajax to pass data.
This is my ajax part, I put the parameter in URL, but php code doesn't read it.
$.ajax({
type:"POST",
url:"system_setting.php?p=add",
data:"username="+username+"&password="+password+"&nickname="+nickname+"&authority="+authority,
success:function(data){
viewData();
}
});
I use $page=isset($_GET['p'])?$_GET['p']:''; to judgment parameter, but it doesn't read. Please teach me how should I do, thank you.
Use POST instead and add p=add in data instead url
$.ajax({
type:"POST",
url:"system_setting.php",
data:"p=add&username="+username+"&password="+password+"&nickname="+nickname+"&authority="+authority,
success:function(data){
viewData();
}
});
And in php
$page=isset($_POST['p'])?$_POST['p']:'';
Give this a try:
var data = {};
data.username = username;
data.password = password;
data.nickname = nickname;
data.authority = authority;
data.p = “add”;
$.ajax({
type:"POST",
url:"system_setting.php",
data: data
}).done(function(data){
viewData();
});
Then when you need the data uses $_POST[] e.g. $_POST[‘p’]
Try this,ajax parameter "method" is used for POST and GET.
$.ajax({
method: "POST",
url: "system_setting.php",
data: { p:"add", username: "unsername", password: "password", nickname: "nickname", authority:"authority" },
success:function(data){
alert(data);
}
});
You have to use only POST or only GET.
Working example:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var username="username";
var password="password";
var nickname="nickname";
var authority="authority";
$.ajax({
type:"POST",
url:"system_setting.php",
data:"p=add&username="+username+"&password="+password+"&nickname="+nickname+"&authority="+authority,
success:function(data){
alert(data);
}
});
</script>
In system_setting.php
$page=isset($_POST['p'])?$_POST['p']:'';
echo $page;

jQuery AJAX call to PHP Controller

I am calling a controller (Codeigniter) through jQuery. My dataString variable contains a simple string, which I'm trying to pass through to my controller, so I can pass it into the model. However, I'm getting an error that indicates that my $test_var is undefined. What am I doing wrong?
$('a.test').click(function (event) {
dataString = $(this).attr('name');
$.ajax({
type:"POST",
url:"<?php echo base_url(); ?>controller_name/",
data:dataString,
success:function (data) {
alert('test');
}
});
event.preventDefault();
});
controller
$test_var= $this->input->post('dataString');
Try using a name=value pair:
$('a.test').click(function (event) {
dataString = $(this).attr('name');
$.ajax({
type:"POST",
url:"<?php echo base_url(); ?>controller_name/",
data:'dataString='+dataString,
success:function (data) {
alert('test');
}
});
event.preventDefault();
});
you could also do something like this, which is an alternative syntax
$('a.test').click(function (event) {
dataString = $(this).attr('name');
$.ajax({
type:"POST",
url:"<?php echo base_url(); ?>controller_name/",
data:{'dataString':dataString},
success:function (data) {
alert('test');
}
});
event.preventDefault();
});
in your controller $test_var= $this->input->post('dataString');
or just like in vanilla php
$test_var = $_POST['dataString'];
use data option like this
data: { name: "John", location: "Boston" }
for more details plz check http://api.jquery.com/jQuery.ajax/

How can I get data after click on text with AJAX jquery?

I used this code for sending and returning result.
<script type="text/javascript">
$(document).ready(function() {
$('.special').click(function(){
var info = $(this).attr("rel");
//$(this).html(sku);
$.ajax({
type: "POST",
url:"../ajax/addSpecialFlag.php",
async: false,
data: {info:info},
success:function(result){
$(this).html(result);
}});
});
});
</script>
<b style="cursor: pointer" class="special" rel="<?=$v['number']."/*".$v['vid']; ?>">Special</b>
addSpecialFlag.php
<?php
echo $_POST['info'];
?>
This code should return "Info" in "<-b class='special' ->" but no returning result. Where is the problem ? Thanks in advance !
The problem should be that $(this) inside your success-handler is not the same as outside of the handler. Doing it like this should solve your problem:
$(document).ready(function() {
$('.special').click(function(){
var $this = $(this);
var info = $this.attr("rel");
$.ajax({
type: "POST",
url:"../ajax/addSpecialFlag.php",
async: false,
data: {info:info},
success:function(result){
$this.html(result);
}});
});
});
<b>? Umad? Please use <strong>.
I have tidied your code so the paranthesis match their line indents and adjusted your success handler. The issue was the success handler is in a different scope to the click function scope so you needed to refer to it in another way, i.e. by assigning it to another variable.
$(document).ready(function () {
$('.special').click(function () {
var info = $(this).attr("rel");
var _this = $(this);
//$(this).html(sku);
$.ajax({
type: "POST",
url: "../ajax/addSpecialFlag.php",
async: false,
data: {
info: info
},
success: function (result) {
_this.html(result);
}
});
});
});

How to get json data and display in div using jquery

Let's say .. I have the following script in my controller:
$result= array("Name"=>Charlie Sheen, "Age"=>30);
echo josn_encode($result);
And in my view file I have the following java script. Now could you please tell me what exactly to write in the success function of the script below in order to get the "Name" and "Age" displayed in two different divs inside body tag? I have found some tutorials on this but all those are very confusing to me. Could you please kindly help?
Thanks in Advance :)
<script type="text/javascript">
$(function(){ // added
$('a.read').click(function(){
var a_href = $(this).attr('href');
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>batch/get_info",
data: "id="+a_href,
success: function(server_response){
//What exactly to write here
}
}); //$.ajax ends here
return false
});//.click function ends here
}); // function ends here
</script>
Make sure you spell you json_encode function correctly.
Your html should look like this:
<head>
<script type="text/javascript">
$(function(){ // added
$('a.read').click(function(){
var a_href = $(this).attr('href');
$.ajax({
dataType : 'json', //You need to declare data type
type: "POST",
url: "<?php echo base_url(); ?>batch/get_info",
data: "id="+a_href,
success: function(server_response){
$( '#name' ).text( server_response.Name );
$( '#age' ).text( server_response.Age);
}
}); //$.ajax ends here
return false
});//.click function ends here
}); // function ends here
</script>
</head>
<body>
<div id='name'></div>
<div id='age'></div>
</body>
First of all, you should set the dataType. It should have the value 'json'. As for accessing it, you just access it as you would any other JavaScript object:
<script type="text/javascript">
$(function() {
$('a.read').click(function() {
var a_href = $(this).attr('href');
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>batch/get_info",
data: "id=" + a_href,
dataType: 'json',
success: function(result) {
alert(result.Name); // Charlie Sheen
}
});
return false;
});
});
</script>

Categories