Not getting response in ajax - php

I am trying to get ajax response. But my code does not show anything in alert. Here is my ajax code. my form id is contact_form and my submit button id is submit_btn
$(document).ready(function(){
$('#contact_form').on('submit', function(e){
var form= $("#contact_form").val();
$.ajax({
type : 'POST',
url : 'email.php',
data : form.serialize(),
dataType : 'json',
success: function (response) {
alert(response);
},
error: function(jqXHR, textStatus, errorThrown) {
//console.log(textStatus, errorThrown);
}
});
e.preventDefault();
});
});
my email.php
<?php
echo "ok";
?>

I think that your problem is because you are trying to use the serialize method in a wrong way.
The .serialize() method will handle all the fields that are inside the selected target form. In the original code, You've used the .val() method to take the values of the form, but this will not work because that method is intended to be used with a single form element and can't handle the whole form.
Change your code in this way:
$(document).ready(function(){
$('#contact_form').on('submit', function(e){
e.preventDefault();
var form = $("#contact_form");
$.ajax({
type : 'POST',
url : 'email.php',
data : form.serialize(),
success: function (response) {
alert(response);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
});

Related

Ajax - how to get data from database and put it on text area CKeditor

i have problem to get data from database to put it on my CKeditor value.
because i want to edit data. and i should show data from database
i using ajax to the data
my ajax
function EditNews(id_news)
{
for (instance in CKEDITOR.instances) {
CKEDITOR.instances[instance].updateElement();
}
//Ajax Load data from ajax
$.ajax({
url : "<?php echo site_url('admin-spot/news/EditNews')?>/"+id_news,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('[name="title_news"]').val(data.title_news);
$('[name="id_news"]').val(data.id_news);
$('[name="text_news"]').val(data.text)
$('[name="date"]').datepicker('update',data.join_date);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error get data from ajax');
}
});
}

Receive .ajax in PHP without form

I've got this script:
<script>
var request;
$("#foo").click(function(event){
request = $.ajax({
url: "/votesHandler.php",
type: "post",
data: "true"
});
request.done(function (response, textStatus, jqXHR){
alert("Voted!");
});
request.fail(function (jqXHR, textStatus, errorThrown){
alert(
"Oops, something went wrong"
);
});
request.always(function () {
alert("Done.");
});
</script>
And what I'm trying to do is send "true" to the server when the #foo element is clicked. I've used AJAX only a few times before and each time it was with forms. The problem is that I need to receive this POST request in PHP (with $_POST['foo']) but this time the input is not the name of a text input. How do I send data into $_POST without a form?
You can specify the key of data when you send to your url.
$.ajax({
data: {foo: 'true'}
});
To retrieve it use
$_POST['foo']
Send it as:
data: 'foo_clicked=1',
or
data: 'foo_clicked=' + somevarname,
On the PHP side:
$recd = $_POST['foo_clicked'];
if ($recd == 1) {
//do what you need to do
}
try this:
$("#form").submit(function (e) {
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
console.log(formURL); //your form action
$.ajax({
url: formURL,
type: "POST",
data: postData,
beforeSend: function () {
//do
},
success: function (data, textStatus, jqXHR) {
console.log('success data');
//whatever
},
error: function (jqXHR, textStatus, errorThrown) {
//if fails
console.log('fail');
}
});
e.preventDefault(); //STOP default action
});

PHP script not running from Jquery/AJAX?

I am trying to pass the user's checked HTML radio button value to a PHP variable using Jquery/Javascript and Ajax.
The following is a simplified version of the HTML/Javascript:
$("input[name=bus_plan]").on('change', function(){
var $postID = "=" + $('input:radio[name=bus_plan]:checked').val();
$.ajax ({
type: "GET",
url: "product-group.php",
data: {"postID" : $postID },
success : function(data){
alert("done");
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("problem: " + errorThrown);
}
});
});
The ajax call shows a success (i.e. there is a "done" alert.)
This is the product-group.php:
<?php
echo "hello world<br>";
$postid = $_GET['postID'];
echo "The postID is ".$postid;
?>
Any help in understanding/fixing the fact that product-group.php does not appear to run would be most appreciated.
Thank you.
Try alert(data) instead of alert("done") to see if jQuery is receiving the correct response.
Your echo output is in "data" var, its like the "return" for ajaxcalls.
Tru alert(data) or append the data content on some div.
Try rewriting your ajax call like this-
$("input[name=bus_plan]").on('change', function(){
var postID = $('input:radio[name=bus_plan]:checked').val();
$.ajax ({
type: "GET",
url: "product-group.php?postID="+postID,
success : function(data){
alert("done");
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("problem: " + errorThrown);
}
});
});

How to get each value from json method

ajax
$('#stb_no').blur(function(){
var stb_no= $('#stb_no').val();
$.ajax({
url: "http://localhost/paymybill/ajax/stb_info",
global: false,
type: "POST",
data: {
'stb_no':stb_no, // you should give a key to the variable
},
success: function(data) {
$('#amount').val(data);
// $(".email_msg").addClass("red");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
Controller code
public function stb_info(){
$stb_no=$this->mso->alldata_stbno($this->input->post('stb_no'));
echo json_encode($stb_no);
}
i am getting out put
[{"sxb_no":"xxxxxx","mzo_name":"xx","cto_name":"xxxxx","area":"xxxxx","name_sxb_owr":"","mobile_no":"xxxxxx","email":"xxxxx#yahoo.com","amount":"xxx"}]
i need to know how to get each values ex:- if i want get email id what i should i do in jquery please help me i new to ajax
Most browsers support JSON.parse(), which is defined in ECMA-262 and is the recommended way. Its usage is simple (I will use your example JSON):
var json = '{"area":"xxxxx",...,"email":"xxxxx#yahoo.com","amount":"xxx"}';
var obj = JSON.parse(json);
Note that obj.email can't be used, because you are parsing an array.
Edit: Checking your comments you need to know that the data parameter is the JSON object, parse it first and then you can do:
$('#amount').val(obj[0].email);
Just add to $.ajax call parameter dataType:"json" and Jquery will parse it in success parameter automatically. Then use it data[0]['email'];
For example :
$('#stb_no').blur(function(){
var stb_no= $('#stb_no').val();
$.ajax({
url: "http://localhost/paymybill/ajax/stb_info",
global: false,
type: "POST",
data: {
'stb_no':stb_no, // you should give a key to the variable
},
success: function(data) {
$('#email').val(data[0]['email']);
//OR
var obj = JSON.parse(data);
$('#email').val(obj[0].email);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});

jQuery ajax on multi-step form

Hi everyone I'm trying to incorporate jQuery AJAX on my multi-step form so that it updates a certain div to the one on the process.php page but it keeps loading the results on a new page. How can I get it to update the div without refreshing the page?
This is the jQuery code I'm using:
$.ajax({
url: 'process.php',
data: $('form[name="booking"]').serialize(),
dataType: 'html',
type: 'POST',
success: function(data) {
// this is the bit that needs to update the div
$('#last-step').fadeOut(300, function() { $('#result').html(data).fadeIn(300);
},
complete: function (data) {
// your code here
},
error: function (url, XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + errorThrown);
}
});
This is the code for my multistep form: http://jsfiddle.net/xSkgH/47/.
Many thanks in the advance
I dont see a div called result in your Markup. So probably you need to show your result in the last div you are showing. And you are missing }) also. the below code should work,
$.ajax({
url: 'process.php',
data: $('form[name="booking"]').serialize(),
dataType: 'html',
type: 'POST',
success: function(data) {
// this is the bit that needs to update the div
$('#last-step').fadeOut(300, function() {
$('#last-step').html(data).fadeIn(300);
});
},
complete: function (data) {
// your code here
},
error: function (url, XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + errorThrown);
}
});

Categories