how to post multiple values to PHP function using AJAX? - php

I have this code:
var name= "theName";
var surname= $('#surname').val();
$.ajax({
url: 'SaveEdit.php',
type: 'post',
data: { "callFunc1": whichOne},
success: function(response) {
alert(response);
}
});
It's working, but i need to do something like this:
var name= "theName";
var surname= $('#surname').val();
$.ajax({
url: 'SaveEdit.php',
type: 'post',
data: { "callFunc1": whichOne, name},
success: function(response) {
alert(response);
}
});
It gives me errors like:
Warning: missing argument 2 for func1()
I am using this code too:
function func1($data, $name){
//some code here
}
if (isset($_POST['callFunc1'])) {
echo func1($_POST['callFunc1']);
}
How to do it right?

Either create an array and pass as much as variable you want. otherwise try this:-
var name= "theName";
var surname= $('#surname').val();
$.ajax({
url: 'SaveEdit.php',
type: 'post',
data: { "callFunc1": whichOne,"callFunc2": name},
success: function(response) {
alert(response);
}
});

You should add a name to your secund parameter in the AJAX call. Don't forgot to pass it ot your function "func1".
Example:
if (isset($_POST['callFunc1'])) {
echo func1($_POST['callFunc1'], $_POST['callFunc2']);
}

Related

when I send data with ajax php wont get anything

post and get works fine but json returns wrong value , or something is wrong with my php code.
$(function () {
$('#username').on('keypress',function () {
var input = $('#username').val();
if(input.length>=4){
$.ajax({
url:'registration_php.php',
type: 'POST',
data:{username:input},
success:function () {
$.getJSON('registration_php.php',function (text) {
alert(text.user);
});
}
});
}
});
});
success:function(result) {
var items = JSON.parse(result);
alert(items['user']);
}
pass the result directly to your reponse as an argument like this
you should specify a dataType: "json" in your ajax call
var postData = JSON.stringify({
username: 'value'
});
var request = $.ajax({
url: "registration_php.php",
method: "POST",
dataType: "json",
data: postData,
});
request.success(function( results ) {
console.log(results)
});

How to utilize the Ajax call success return data to php code for running mysql query in same file

I am getting data from ajax call. But that data is coming in Jquery and I have saved it in a variable. Now I want that data to be utilized for running some php and mysql code. Can any one solve this?
$("#submit_bt").click(function () {
var name = $('#search-box').val();
var dataString = 'name=' + name;
if (name == "" ){
$('.alert').show().html('Please fill all information')
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "read_data.php",
data: dataString,
cache: false,
success: function (result) {
alert(result);
//$('.alert').show().html(result).delay(2000).fadeOut(3000);
setTimeout(function(){window.location.href = "index.php";},2000);
}
});
}
return result;
});
If what you want is to navigate to the index.php page on click of that button, then, do it this way:
$("#submit_bt").click(function () {
var name = $('#search-box').val();
var dataString = 'name=' + name;
if (name == "" ){
$('.alert').show().html('Please fill all information')
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "read_data.php",
data: dataString,
cache: false,
success: function (result) {
alert(result); //you may remove this. use console.log for debugging your js next time
setTimeout(function(){window.location.href = "index.php?result="+result;},2000); //why the timeout?
}
});
}
});
The easier and proper solution should be to re-use ajax to use this variable in another PHP file.
$("#submit_bt").click(function () {
var name = $('#search-box').val();
var dataString = 'name=' + name;
if (name == "" ){
$('.alert').show().html('Please fill all information')
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "read_data.php",
data: dataString,
cache: false,
success: function (result)
{
//AJAX code to execute your MySQL query
$.ajax({
type: "POST",
url: "read_data2.php",
data: result,
cache: false,
success: function (result)
{
//Manage your read_data2.php output
}
});
}
});
}

undefined php data using ajax

I am trying to send 2 values from cx_card to my getproduto.php. For this I am using ajax
The variable values:
com_id=1
cliente_id=3440
The form select:
<select width="200" name="produto" id="produto-list" class="produtoInputBox" onChange="getsubproduto(this.value);" <?php echo $permissao; ?>>
ajax code
function getproduto(val,val_id) {
$.ajax({
type: "POST",
url: "inc/cx/get_produto.php",
data:'com_id='+val+'&cliente='+val_id,
success: function(data){
$("#produto-list").html(data);
}
});
}
My Query in getproduto.php
$query ="SELECT * FROM apolices WHERE id_cliente='".$_POST['cliente_id']
."' and id_companhia = '".$_POST['com_id']."'";
$results = $db_handle->runQuery($query);
In cx_card i have print the query i have recibe from getproduto.php the result is:
SELECT * FORM apolices WHERE id_cliente='undefined' and id_companhia='1'
Thanks for the help.
You're only passing one argument to your getproduto function. If you want two different values you need two different arguments. Try this:
function getproduto(com_id, cliente_id) {
$.ajax({
type: "POST",
url: "inc/cx/get_produto.php",
data:'com_id='+com_id+'&cliente_id='+cliente_id,
success: function(data){
$("#produto-list").html(data);
}
});
}
You're only sending in one value:
function getproduto(val) {
$.ajax({
type: "POST",
url: "inc/cx/get_produto.php",
data:'com_id='+val+'&cliente_id='+val,
success: function(data){
$("#produto-list").html(data);
}
});
}
You should be sending two:
function getproduto(val,val_id) {
$.ajax({
type: "POST",
url: "inc/cx/get_produto.php",
data:'com_id='+val+'&cliente_id='+val_id,
success: function(data){
$("#produto-list").html(data);
}
});
}

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.

How to use jquery VALUE in an ajax success function

I cannot use value div name in fourth-last line
I want to use div name in Ajax function in $("divname").html(data);
$('.edit').click(function() {
var object = $(this);
var rowvalue = object.attr('id');
var rowvalue_array = rowvalue.split('_');
var id = rowvalue_array[1];
var comment = $('#comment_'+id).val();
var divname = '#'+id;
var varData = 'id='+id+'&comment='+comment;
console.log(varData);
$.ajax ({
type: "POST",
url: "edit_field.php",
data: varData,
success: function(data) {
$("divname").html(data);
}
});
return false;
});
Use the following code..
$.ajax ({
type: "POST",
url: "edit_field.php",
data: varData,
success: function(data) {
$(divname).html(data);
}
});
Please replace your line as below :
$(divname).html(data);
remove quata (")
divname is variable
or you can use as below :
$("#"+id).html(data);
Try this:
$.ajax ({
type: "POST",
url: "edit_field.php",
data: varData,
success: function() {
$("divname").html($(this).data);
}
});
Why are you accessing the element by id when u have the element.....try
$(object).html(data);

Categories