pass string using jquery ajax to php - php

I want to send a hash string to my php file. but failed, can anyone help me please?
my javascript is like this:
var hashString = "#support_form";
$.ajax ({
type: "POST",
url:"index.php",
data: hashString,
success: function() {
console.log("message sent!");
}
});
and the php:
<?php
$theHash = $_POST['hashString'];
?>
What should I do? Thanks

You need to specify the name/value for data
data: {hashString:hashString},

You have to do like this-
$.ajax ({
type: "POST",
url:"index.php",
data: "hashString=value",
success: function() {
console.log("message sent!");
}
});
So you can get the value as-
$theHash = $_POST['hashString'];
echo $theHase; //will print value

Use:
data: 'hashString=' + encodeURIComponent(hashString),

Your code would work if you used an object instead of a string. try this:
var hashString = {hashString: "#support_form"};
$.ajax ({
type: "POST",
url:"index.php",
data: hashString,
success: function() {
console.log("message sent!");
}
});

Your JS should be this:
var hashString = "#support_form";
$.ajax ({
type: "POST",
url:"index.php",
data: {hashString: hashString},
success: function() {
console.log("message sent!");
}
});

data key must be of object type
This will work for you
var hashString = "#support_form";
$.ajax ({
type: "POST",
url:"index.php",
data: {'hashString':hashString},
success: function() {
console.log("message sent!");
}
});

$.ajax ({
type: "POST",
url:"index.php",
data: {"hashString":hashString},
success: function() {
console.log("message sent!");
}
});

var queryString = "f_name=" + f_name + "&l_name=" + l_name + "&contact_no=" + contact_no + "&email=" + email;
$.ajax({
type:'POST',
url:'insert1.php',
data:queryString,
dataType:'json',
success: function(success){
console.log("message sent!");
}
});

Related

How to pass json data to html form?

I am trying to pass json data after successfully received the array, but what i am trying to pass to modal window to edit the values. data is array actually have the values but not passing to the html form.
function passvalue(val,start,end)
{
var id=val;
var start=start;
var end =end;
//var dataString = "id=" + id;
$.ajax ({
type: "POST",
url: "get-results.php",
data: {id:id},
// dataType: 'json',
success: function(data)
{
//console.log(data);
$('#ctemail').val(data.cemail);
}
});
}
And my html is:
Enter Customer Email
I corrected some shortcomings.
function passvalue(val,start,end)
{
var id=val;
var start=start;
var end =end;
//var dataString = "id=" + id;
$.ajax ({
type: "POST",
url: "get-results.php",
data: 'id='+id, // multiple: data: 'id='+id+'&id2='id2,
// dataType: 'json',
success: function(json_data)
{
var data = JSON.parse(json_data); // Json parse
//console.log(data);
$('#ctemail').val(data.cemail);
}
});
}

How to save ajax post response in to database?

My code is here.
<script type="text/javascript">
function test(){
alert('return sent');
$.ajax({
type: "POST",
url: "http://remotewebsite.com/process.php",
data: somedata;
dataType:'text'; //or HTML, JSON, etc.
success: function(response){
alert(response);
//echo what the server sent back...
}
});
}
</script>
But how can i save that response coming from remote website in to my database?
And how can i send multiple input?
$.ajax({
type: "POST",
url: "http://remotewebsite.com/process.php",
data: somedata;
dataType: 'text'; //or HTML, JSON, etc.
success: function(response) {
alert(response);
$.ajax({
type: "POST",
url: "youphppagewhereyoudothesavingindatabase",
data: response;
dataType: 'text'; //or HTML, JSON, etc.
success: function(data) {
console.log(data);
alert("Saved in database")
}
});
}
});
Have it something like this

ajax post with PHP

why isn't this working?
jQuery AJAX Code:
$("header input").bind("keyup", function()
{
var searchString= $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false
});
});
PHP Code(Just a test Code):
if($_POST["search"]) {
echo "TEST MESSAGE!";
}
It doesn't show The echo :/
thanks for ur help ;)
You need to display the data you receive from the ajax call.
Example, to put the result into a <div> called YourresultDiv:
Try with this
$("header input").on("keyup", function () {
var searchString = $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
success: function (data) {
$('#YourresultDiv').html(data);
alert("Successful");
}
});
});
Hopes this will help you....
$("header input").bind("keyup", function()
{
var searchString= $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
async: false
},success: function (data) {
$('div#posteddata').append(data);
}
);
});
<html>
<head>
</head>
<body>
<div id="posteddata"></div>
</body>
</html>
You need to specify an element you want to update.. for example
<div id="result"> </div>
and then append a success event handler to your ajax call
$("header input").bind("keyup", function () {
var searchString = $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false
}).success(function (data) {
$("#result").html(data);
}).fail(function () {
alert("Ajax failed!");
});
});
Try with this
In js add
success: function (data) {
// success handler
}
as your response handler
if($_POST["data"]) {
// search = search string available here in $_POST['data']
echo "TEST MESSAGE!";
}
where is your call back function in $.ajax() function,with callback function only,you can display anything through an ajax request..
So try this.
$("header input").on("keyup", function () {
var searchString = $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
success: function (data) {
$('#Yourdiv').html(data); // or $('#Yourdiv')text(data);
}
});
});
Without success function,you can see the echoed statement in your network segment in console.
for that, press F12,then you will get a link like
XHR finished loading: POST "http://localhost/yourproject/func_name.Click on that link and you will goto network segment and from there,click on the function name and then in response or preview tab,you canb see the echoed statement..
try it.

AJAX is not calling PHP?

Can anyone please point out exactly what I am doing wrong, I am trying to make a php call when a selected value is changed.
The code is not echoing the information from the php file.
JQUERY CODE
// Skill sort on change
$('#order_by').on('change', function() {
$.ajax({
type: "POST",
url: "sort_skill_be.php",
data: {skill:this.value}
}).done(function(result){
console.log(result)
})
});
PHP CODE
<?php
session_start();
$skill_sort = $_POST['skill'];
echo $skill_sort;
echo 'I got in here';
?>
Thank you for the help and time!
EDIT: It works correctly now, Thanks for all the help!
Try this:
$('#order_by').on('change', function() {
var sk = $(this).val();
$.ajax({
type: "POST",
url: "sort_skill_be.php",
data: 'skill=' + sk,
success: function(result) {
alert(result);
}
});
});
Try this
$('#order_by').on('change', function() {
$.ajax({
type: "POST",
url: "sort_skill_be.php",
data: {skill:$(this).val()},
success: function(result){
alert("done");
},
error: function(){
alert("error");
}
});
});
You should use then instead of done http://promises-aplus.github.io/promises-spec/
$('#order_by').on('change', function () {
$.post("sort_skill_be.php", {
skill: $(this).val()
}).then(function (result) {
console.log(result);
}).fail(function () {
console.err('failed to fetch data');
});
});
You could test things out like this...
// Skill sort on change
$('#order_by').on('change', function() {
$.ajax({
type: "POST",
url: "sort_skill_be.php",
data: {skill:this.value}
}).done(function(result){
console.log('my results' + results);
})
});

ajax link json datatype call

I want to send the data via ajax to other page. I have isolated the problem. This is the code.
Thank you all for your help..But no effect..
updated code
It worked...
<script>
$(document).ready(function(){
$(".edit").click(function(event) {
event.preventDefault(); //<--- to prevent the default behaviour
var box = 1233;
var size=123;
var itemname=123;
var potency=123;
var quantity=12333;
var dataString ={
'box' :box,
'size':size ,
'itemname':itemname,
'potency':potency,
'quantity':quantity
};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
success: function(data) {
alert(data);
},
error: function(data) {
alert(data);
}
});
});
});
</script>
So I click the link,it navigates, to dd.php which has
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']));
echo $_POST['itemname'];
?>
I get Object Object as alert. What am doing wrong? Pls throw some light here..thanks you..
$(document).ready(function(){
$(".edit").click(function(event) {
event.preventDefault();
var data = {"box":1233,
"size":565,
"itemname":565,
"potency":876,
"quantity":234};
$.ajax({
url: "dd.php",
type: "post",
data: data,
dataType: "json",
success: function(data) {
if(console){
console.log(data);
}
},
error: function(data) {
if(console){
console.log(data);
}
}
});
});
});
few things to consider... you can post data as object..which is clean and easier to use
$(".edit").click(function(event) {
event.preventDefault(); //<--- to prevent the default behaviour
var box = 1233;
....
var dataString ={'box':box,'size':size,'itemname':itemname,'potency':potency,'quantity':quantity};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
dataType: "json", //<--- here this means the response is expected as JSON from the server
success: function(data) {
alert(data.itemcode); //<--here alert itemcode
},
error: function(data) {
alert(data);
}
});
so you need to send the response as json in PHP
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']))
?>
Here you are using querystring as sent in GET request.
If you want to send the data in same form, you can use this with GET request type:
$.ajax({
url: "dd.php"+dataString,
type: "get",
dataType: "json",
success: function(data) {
console.log(data);
alert(data.itemcode);
},
error: function(data) {
alert(data);
}
});
Or for POST request,you will have to put data in json object form, So you can use :
var dataString ={
'box' :box,
'size':size ,
'itemname':itemname,
'potency':potency,
'quantity':quantity
};
$.ajax({
url: "dd.php",
type: "post",
data: dataString,
dataType: "json",
success: function(data) {
console.log(data);
alert(data.itemcode);
},
error: function(data) {
alert(data);
}
});
});
And put echo in your php code :
<?php
echo json_encode(array('itemcode'=>$_POST['itemname']))
?>
Javascript alert shows [Object object] for object. You can see response using console.log or can use that key with alert.
For more information, refer jQuery.ajax()

Categories