passing parameter with js/ajax to a php document - php

$(document).ready(function()
{
$(":input").focusout(function () {
var a= $(this).closest("tr").attr('id');
var b= $(this).closest("td").attr('id');
var data = $(this).attr("value");
$.post("database.php",
{trAdress: a, tdAdress: b }, function(data){ alert("Data Loaded: " + data); }); });});
I want to pass trAdress and tdAdress also data parameters to a php document... Can anyone help me how can I get these parameters in a php document_?

They will be set in the $_POST array as $_POST['trAdress'] and $_POST['tdAdress'].

If you sent them using POST, you can access the values sent in the form using $_POST superglobal variable:
<?php
$trAddress = $_POST['trAddress'];
$tdAddress = $_POST['tdAddress'];
// your code here
http://php.net/manual/en/reserved.variables.post.php

$_POST["trAdress"] and $_POST["tdAdress"] will get you the values

Related

How to set the ajax success result value to PHP variable in same page

function submitID(){
// some codes working in here....
var text="A_1";
var name="test";
$.ajax({
url: 'callmethod.php',
type: 'post',
data: {'action':'methodname', 'text': text, 'name':name},
success: function(data, status) {
var results = JSON.parse(data);
for(var i = 0; i < results.length; i++){
var id= results[i]['ID'];
alert(id); --- I got the value of ID in here -- example output value : Hello
}
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
}
What I would like to do in here, I got the value of var id but I want to set as the PHP variable $valuephp= >>> id <<< how can I do for that? please help me to advice.
In short, its not possible, like that to set ajax response to php variable because
PHP runs - Server Side before page load and builds the page
JS (and thus AJAX) runs - Client Side AFTER the server gave your browser the page
So what you can do ?
You post data to your server with your ajax call, so in server side, you can set php variable.You can store this in a session variable if you need it later, or in the database.
You can store ajax result in php variable by embedding ajax result div into php tag.
Assign ajax response variable to js variable and then just assign it to your html div containing php variable as:
$('#result').text('test');
<?php
$result='<div id="result"></div>';
// Here you embed div in php tag and store in php varible
?>
In your ajax call, after success or error
<?php $variablephp = "<script>document.write(111)</script>" ?>
Now you can access that variable, make sure that variable actually used after ajax call
<?php echo $variablephp; ?>

passing an array to a .php file using post in jquery

I am trying to post an existing array in jquery using $.post to a evaluate.php page. I tried to serialize it and send it. But its not working. I get a internal server error. Can somebody suggest a method for me to post my array? Very much in need of help.
<?php
function radioevaluation($qnum) {
echo "<script> $(document).ready(function(){
var x= ".$qnum.";
$('#q'+x+'verify').click(function(){
var answerarray = [];
answerarray['answer'] =$('input[name=q'+x+']:checked').val();
answerarray['qnum']= x;
var answer =jQuery.param(answerarray);
$.post('evaluate.php',answer,function(data){
var a= data;
alert(a.result);
}, 'json');
});
});</script>";
}
?>
EDIT:
Sorry I missed your second comment.
I think you want to define answerarray not as an array but like so:
var answerarray = {};

Passing var using jquery & ajax

I am trying to pass the value of a html textbox form field to another .php file using jquery and ajax when the user defocuses that textbox. The textbox has the id "aircraftReg". I am using the code as follows...
$(document).ready(function(){
$("#aircraftReg").blur(function() {
var aircraftReg = $(this).value;
$.get("searchDatabase.php?q=aircraftReg", function(data){
// My function
});
});
});
I think the problem lies in creating the var aircraftReg. I am attempting to assign its value to the text within the text box with id "aircraftReg".
Can anyone see what is going wrong?
Try to change it like this:
var aircraftReg = $(this).val();
$.get("searchDatabase.php?q="+aircraftReg , function(data){
// My function
});
To get value of text field (or other input) there is .val() method in jQuery.
You need to change your Url because you are passing javascript var to url so it must me added with +.
var aircraftReg = $(this).val();
$.get("searchDatabase.php?q="+aircraftReg , function(data){
//function logic goes here
});
You have passed aircraftReg as regular string not as a javascript variable.
$(document).ready(function(){
$("#aircraftReg").blur(function() {
var aircraftReg = $(this).value; //or $(this).val();
$.get("searchDatabase.php?q="+ encodeURIComponent(aircraftReg), function(data){
// My function
});
});
});
Also, you have three encoding options:
escape() will not encode: #*/+
encodeURI() will not encode: ~!##$&*()=:/,;?+'
encodeURIComponent() will not encode: ~!*()'
Note: escape() function is non-ASCII, encodeURI() and encodeURIComponent() are UTF-8 compatible.

How do I get jquery variable value to be read by PHP $_POST?

I'm able to successfully get all the values from a multi-select form into one nice delimited variable, but I can't figure out how to get the value to my PHP script? How do I get the 'output' value read by PHP's $_POST array? Any help would. Be. Awesome. :D
<script type="text/javascript">
function ValidatePageForm() {
var result = new Array();
$("#select-to option").each(function() {
result.push($(this).val());
});
var output = result.join("-");
alert(output);
}
</script>
suppose you have a form
<form>
<input type="hidden" name="output" id="output">
....
</form>
send javascript variable to HTML
var output = result.join("-");
$('#output').val(output);
and when you submit the form
you wil get data in $_POST['output']
I believe your looking for something like echo $_POST['value']; ??
You can use Jquery serialize to post all the data of the form including multi select
var submit_data = $('#output').serialize();
var post_data = submit_data ;
$.ajax({
type: "POST",
url: 'submitform.php',
data: post_data,
success: function(data)
{
}
});
You will get all the value in $_POST on submitform.php
Let me know it works for you

Passing HTML INPUT field ID to PHP completion program in jQuery Autocomplete

I want to pass the id of the INPUT field to the PHP file providing options. Here's my HTML & jQuery code. But the PHP program gets the id as undefined. Thanks for helping.
jQuery :
$('.classfield').autocomplete({
//define callback to format results
source: function(req, add){
//pass request to server
$.getJSON("ajax/ajax_suggestions.php?id="+$(this).attr('id')+"&callback=?", req, function(data) {
//create array for response objects
var suggestions = [];
//process response
$.each(data, function(i, val){
suggestions.push(val.name);
});
//pass array to callback
add(suggestions);
});
},
//define select handler
change: function(e) {
$("#spill").html("change "+$(this).val()+e.type);
}
}); // autocomplete
HTML:
<input type="text" class="classfield" id="hello" value="there"></input><br>
the value of $(this).attr('id') is undefined because this is the object that is put in the parameter of autocomplete (the parameter of autocomplete accepts an object, so if you use $(this).attr('id'), you are referencing the object that was passed in the parameter on the autocomplete)
therefore you cannot use $(this).attr('id').
You have to store the id of the text field, may be as a global variable... Hope this helps a little bit
Try getting the id from this.element:
//pass request to server
$.getJSON("ajax/ajax_suggestions.php?id="+this.element.attr('id')+"&callback=?", req, function(data) {
Also see this example.

Categories