I am trying to pass a Javascript variable to a PHP file using AJAX.
I have the below Javascript code;
<script type="text/javascript">
var route_id = 'travelling-from'; //Route ID
$('#'+route_id).change(function(e) {
//Grab the chosen value on route change
var selectroute = $(this).val();
$.ajax({
type: "GET",
url: 'ajax-getvalues.php',
data: { selectroute : selectroute }
});
});
</script>
In my ajax-getvalues.php, I have;
$selectroute = mysqli_real_escape_string($connection, $_GET['travelling-from']);
When I try to use $selectroute, it seems to be empty.
Do I need to add something else in order for this to work? Or have I gone wrong at some point?
When I try to use $selectroute, it seems to be empty
The AJAX request will be sent to ajax-getvalues.php with the query string:
?selectroute=somevalue
In PHP you are trying the get the value of a parameter called travelling-from, this parameter does not exist in the query string.
You need to change selectroute to travelling-from
$.ajax({
type: "GET",
url: 'ajax-getvalues.php?travelling-from=' + encodeURIComponent(selectroute)
});
Or of you prefer:
$.ajax({
type: "GET",
url: 'ajax-getvalues.php',
data: {"travelling-from": encodeURIComponent(selectroute)}
});
This produces the query string ?travelling-from=somevalue which can now be accessed with $_GET['travelling-from']
In your example the key should be route_id instead of selectroute
<script type="text/javascript">
var route_id = 'travelling-from'; //Route ID
$('#'+route_id).change(function(e) { //Grab the chosen value on route change var selectroute = $(this).val();
var data = {};
data[route_id] = selectroute;
$.ajax({
type: "GET",
url: 'ajax-getvalues.php',
data: data }
}); </script>
Related
My Javascript Code is like this :
$(".booking_hotel").click(function(){
var id = this.id;
$.ajax({
type: "POST",
url: base_url + 'hotel_booking/hotel/hotel_booking',
data: id
});
});
Function in my controller is like this :
function hotel_booking()
{
$data = $this->input->post();
$data = array(
'hotel_request' => $data,
);
$this->load->view('hotel_booking/booking_hotel_view',$data);
}
It not successful to load view. But in console firebug(tab html), the view could appear.
How to load view booking_hotel_view when class .booking_hotel clicked?
Thank you
The view from ajax comes to you as ajax response. You have to put it in your desired place.
Let you have div with id="my_div" and you want to show your view in this div. So your code will be
$(".booking_hotel").click(function(){
var id = this.id;
$.ajax({
type: "POST",
url: base_url + 'hotel_booking/hotel/hotel_booking',
data: id
}).done(function(response){
$('#my_div').html(response);
});
});
try this. It will reload/redirect to you hotel_booking/hotel/hotel_booking. i.e load your booking_hotel_view
$(".booking_hotel").click(function(){
var id = this.id;
$.ajax({
type: "POST",
url: base_url + 'hotel_booking/hotel/hotel_booking',
data: id
}).done(function(response){
window.location.href = "hotel_booking/hotel/hotel_booking";
});
});
As you doing nothing with your ajax post data you may use as
$(".booking_hotel").click(function(){
window.location.href = "hotel_booking/hotel/hotel_booking";
});
I have a select list where in using javascript i get the selected values i want this selected values to pass through php file init.php so that i can use those variables in mysql query.
my javascript code is as follows:
$(document).ready(function(){
var e = document.getElementById("product");
var pro = e.options[e.selectedIndex].text;
alert(pro);
});
$('select').change(function(){
var e = document.getElementById("city");
var cit = e.options[e.selectedIndex].text;
alert(cit);
I have used ajax to send variables to init.php. my ajax code below is not working,can anyone tell whats the issue in this code:
$.ajax({
url: 'init.php',
type: 'POST',
data: { x:'cit',y:'pro' },
success: function(data) {
console.log(data);
}
});
and in init.php i have written :
<?php
$var1 = $_POST['y'];
$var2 = $_POST['x'];
$result = "Select amount from ". _DB_PREFIX_ ."demo_detail where product = '". $var1 ."' and city = '" . $var2 . "' ";
//echo json_encode($result);
Can you alter the url line to include the / to make sure that you're referring init.php relative to the root of your directory?
So it should look like this:
$.ajax({
url: '/init.php',
type: 'POST',
data: { x:'cit',y:'pro' },
success: function(data) {
console.log(data);
}
});
I don't know enough to say for sure but there's a chance that AJAX is making a POST request to the wrong URL.
Have you tried to pass absolute as well as relative path in url. What I mean is
have you tried using:
url:'localhost:xxxx/myapp/init.php'
or
url:'/init.php'
If you're passing variables into the data parameter in Ajax, they don't have to be in quotes.
$.ajax({
url: 'init.php',
type: 'POST',
data: { x: cit, y: pro },
success: function(data) {
console.log(data);
}
});
Use Query String. HTML5's Session Storage can also help you.
Try to replace your script code with following and see if it makes a difference
$(document).ready(function(){
$('select').change(function(){
var e = document.getElementById("product");
var pro = e.options[e.selectedIndex].text;
alert(pro);
var e = document.getElementById("city");
var cit = e.options[e.selectedIndex].text;
alert(cit);
$.ajax({
type: 'POST',
url: 'init.php',
data: { x:'cit',y:'pro' },
success: function(data) {
console.log(data);
}
});
});
});
I've written one AJAX function code as follows :
$('#form').submit(function(e) {
var form = $(this);
var formdata = false;
if(window.FormData) {
formdata = new FormData(form[0]);
}
var formAction = form.attr('action');
$.ajax({
type : 'POST',
url : 'manufacturers.php',
cache : false,
data : formdata ? formdata : form.serialize(),
contentType : false,
processData : false,
success: function(response) {
if(response != 'error') {
//$('#messages').addClass('alert alert-success').text(response);
// OP requested to close the modal
$('#myModal').modal('hide');
} else {
$('#messages').addClass('alert alert-danger').text(response);
}
}
});
e.preventDefault();
});
Now here in data attribute I want to send some additional parameters with values in data attribute. How should I send these parameters to PHP file?
For clear understanding of my issue refer the following AJAX function code that I've written previously :
function GetPaymentRequest(status){
var status = $('#status_filter').val();
$.ajax({
type: "POST",
url: "view_payment_request.php",
data: {'op':'payment_request_by_status','request_status':status},
success: function(data) {
// alert(data);
}
});
}
In above function code you can see that I've passed few parameters with values viz. 'op':'payment_request_by_status','request_status':status in data attribute.
Exactly same parameters I want to pass in first AJAX function code. The already mentioned parameter "formdata ? formdata : form.serialize()" should also be there.
How should I do this? Can someone please help me in this regard?
Thanks in advance.
Add by using $.param
form.serialize() + '&' + $.param({'op':'payment_request_by_status','request_status':status});
or use serializeArray() and push new items
var data = form.serializeArray();
data.push({name:'op',value:'payment_request_by_status'}).push({name:'request_status',value:status});
then pass data
What you can do is, add two hidden fields to your already existing form, name one of them as op and set the value as payment_request_by_status and another one as request_status and the value based on the status.
When the form is serialized, it will automatically send these values also.
Hi I am facing problem with json data. Here is my js code.
<script>
$(function(){
$.ajax({
url:"http://example.com/salary?from=USD&to=GBP",
dataType: 'jsonp',
success:function(json){
alert(json['to']);
},
error:function(){
alert("Error");
},
});
});
</script>
I want to use json data in PHP in same page.
I know that you cannot assign Javascript value to PHP variable.
Is there way to do this?
Or is possible to do similar task in php (Jquery Ajax cross domain) like above javascript code ?
Any help?
your js code
var my_json_obj = new Object();
my_json_obj .name = "Lanny";
my_json_obj .age = "25";
my_json_obj .location = "China";
var json_str = JSON.stringify(my_json_obj);
<script>
$(function(){
$.ajax({
type: "POST",
dataType: "json",
url: "my.php",
data: {
postData: json_str
},
success: function (data) { alert(data) },
eror: function (data) { alert(data) }
});
});
</script>
your my.php file
$postData=$_POST['postData'];
$my_obj=json_decode($postData,true);
$name=$my_obj['name'];
$age=$my_obj['age'];
$localtion=$my_obj['location'];
You could do that with AJAX.
You need a script, which will give javascript vars to the PHP Script, like:
var PHPFile = 'PHPFile.php?arg1=' + arg1 + '&arg2=' + arg2;
In the "PHPFile.php" you can access them by
$arg1 = $_GET["arg1"];
$arg2 = $_GET["arg2"];
Or you could do that with jquery $.ajax-> data, too.
You can access them by
$arg1 = $_POST["arg1"];
$arg2 = $_POST["arg2"];
Something like that:
result = $.ajax({
type: 'POST',
async: false,
url: 'PHPFile.php',
data: ({
arg1: arg1,
arg2: arg2
})
}).responseText;
alert(result);
EDIT:
If you want to do that with a json-object, try this:
json_decode();
http://www.php.net/manual/en/function.json-decode.php
http://www.php.net/manual/en/function.json-encode.php
i have this working code with this code i can pass data to div within same page
but insted of passing data to div i want to pass this to label or textbox so that i can post it to the server. i am new in ajax,jquery . please suggest me best answer
<script type="text/javascript">
//send clicker data to div
$(function() {
$(".clicker").mouseover(function(){
var data = $(this).attr('id');
$.ajax({
type: "POST",
data: "db_data=" + data,
success: function(){
//alert(data);
$('.responseDiv').text(data);
}
});
});
});
</script>
<script type="text/javascript">
i think i need to change this line only
$('.responseDiv').text(data);
but dont know how to do that. didnt find any solution on net also
take any name like propertyId
Let's say the text field has id="propertyID"
Use the val() method to assign this new value to your text field.
$('#proertyID').val(data);
Like:
$(function() {
$(".clicker").mouseover(function(){
var data = $(this).attr('id');
$.ajax({
type: "POST",
data: "db_data=" + data,
success: function(){
//alert(data);
// $('.responseDiv').text(data);
$('#proertyID').val(data);
}
});
});
});
In the below line change selector .responseDiv with id/name or class of input/label $('.responseDiv').val(data);
<script type="text/javascript"> //send clicker data to div
$(function() {
$(".clicker").mouseover(function(){
var data = $(this).attr('id');
$.ajax({
type: "POST",
data: "db_data=" + data,
success: function(data2){
//alert(data);
$('.responseDiv').text(data2);
}
});
});
});
success return in data2 and use this..