Posting variable from JavaScript to PHP? - javascript

I have been trying to post a JS variable into PHP with using the $.ajax function. It didn't work though. The situation is like this: There is a page with a submit button, if I the button is clicked it loads a JS function which has confirm() in it. If I click OK (if confirm is true) it should execute the post action on another php page.
function sure () {
var conf = confirm(text here);
$.ajax({
url: 'action.php',
type: Post
data: {istrue: conf};
})
. }

It's jQuery related than JavaScript.
There is not type param in $.ajax. You have to use method param instead.
$.ajax({
method: "POST",
url: 'action.php',
data: {istrue: conf}
})

Related

Javascript run php file and then download file

I have a page where I would like a form to be completed , a javascript file verifies the information, sends the information to a php file to mail it to me and then initiates a download.
The problem is that I can have the javascript file either send the mail or allow the file to be downloaded, but not both.
The code in question looks like this:
else {
document.dlform7.action = "http://www.myurl.com/c/post7.php" ;
document.dlform7.submit();
window.location.assign(url);
}
Is there a way (perhaps using load) that I can have both of these actions take place at once?
edit:
ajax option:
else {
$.ajax({
url: "http://www.myurl.com/c/post7.php";
type: "POST",
data: data,
cache: false,
success: function () {
window.location.assign(url);
document.dlform7.submit();
},
I think that you need to adjust the submit handler for your form. IRC, the submit event will cause the page to reload.
You will want to create an submit event handler for the form that will make an ajax call to your php script. Then have it return false to prevent the page reload. You should then be able to cause the file download at the same time.
UPDATE
If you want to download the file no matter what happens with the php script to send the email you can do the following:
else {
$.ajax({
url: "http://www.myurl.com/c/post7.php";
type: "POST",
data: $(document.dlform7).serialize(),
cache: false,
});
window.location.assign(url);
The ajax method is the form submission. It doesn't break the flow of your code so the redirect would happen after the request. (I am pretty sure that this will work).
If you want to wait until the php script successfully runs then you would move the code into the success callback like so:
else {
$.ajax({
url: "http://www.myurl.com/c/post7.php",
type: "POST",
data: $(document.dlform7).serialize(),
cache: false,
success: function () { window.location.assign(url); }
});
Of course, you could also just change the php page to send an email and then respond with the file that you want to download as mention in the comments.

Passing variables to PHP script via AJAX

I am truing to pass variables to a PHP script using onclick but obviously I am doing something wrong.
So..I have in my main page somewhere:
<img src="myImage.jpg" id="cart_icon" onclick="addcart('100')">
and the addcart function:
function addcart(id){
$.ajax({
url: "add_item.php",
method: "POST",
data: {prod_id : id}
});
}
The add_item.php looks like this (just a simple example):
if (!isset($_SESSION)) session_start();
if (isset($_POST['prod_id'])){$_SESSION['item_id']=$_POST['prod_id'];}else($_SESSION['item_id']='Not Set');
When I check the value of the SESSION['item_id'] I am getting 'Not set' instead of '100'
Any thoughts? This is just a simple example. The actual code is more complex. Thanks
Firstly, your AJAX call looks incorrect because the method of the call needs to be defined via type and not method.
Read the documentation of $.ajax.
The correct call should be:
function addcart(id){
$.ajax({
url: "add_item.php",
type: "POST", // notice the change over here
data: {prod_id : id}
});
}

No ajax response in laravel 4

I'm taking an input from user named as category. On every key up i want to perform an ajax request to search if category with the similar name exists or not. If yes, then a there is a blank div below that input which i want to fill with the similar name categories.
So i created a form. Used ajax via jquery but i'm not receiving the response.
This is the jquery ajax code that i'm using
$("#category").keyup(function () {
$.ajax({
url: "categories/categoryajaxrequest",
type: "get",
data: {
category: $("#category").val()
},
success: function (response) {
$("#category_suggestion").html(response);
}
});
});
This is the route part which is used as url in ajax request
Route::get('categories/categoryajaxrequest', array('as' =>'categoryajaxrequest', 'uses' =>'CategoryController#categoryAjaxRequest'));
and this is the controller part
//app/controllers/CategoryController.php
....
public function categoryAjaxRequest(){
echo "working";
}
So, the input text has an id of "category" and below the input, there is a div with an id of "category_suggestion". At every keyup on the input, i expect it to perform an ajax request and show "success" in div . But it's not doing anything.
However, if i go directly to "/categories/categoryajaxrequest" on my browser, it outputs "working".
Try putting the action directly in the url: property of the $.ajax() method, then no route is needed:
Laravel gives us the liberty to do that. The curly braces {{ }} are used in views. As the JavaScript stuff is usually called in a view, it is legitim to do it that way.
The URL::action part is described in the docs as follows:
To generate a URL to a controller action, we may use the URL::action
method or the action helper method:
$url = URL::action('FooController#method');
$url = action('FooController#method');
So the combination of the template system & the URL::action gives us the possibility of doing it this way:
$("#category").keyup(function () {
$.ajax({
url: "{{URL::action('CategoryController#categoryAjaxRequest')}}",
type: "get",
data: {
category: $("#category").val()
},
success: function (response) {
$("#category_suggestion").html(response);
}
});
});

Post form to web2py function using ajax

The server is written in web2py, and hosted on google app engine. I can visit my index.html by entering domain.com/index and I can send form by entering domain.com/register where "register" is a function defined by default.py
However, in html, where I would like to send form to the server and get a response, I use ajax which has cross domain issues. So I use "register" as URL, and it does not work. Help?
$("#signup").click(function() {
$.ajax({
type: "POST",
url: "register",
data: $("#formsignup").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data);
}
});
return false; // avoid to execute the actual submit of the form.
});
By typing domain.com/register, I can totally trigger the function. What is the problem here? And the form is sent to domain.com... In browser it appears as htt[://domain.com/?email=ada#ad.com&password=adsa
Its very possible register is looking for GET instead of POST
try changing the type in ajax
$("#signup").click(function() {
$.ajax({
type: "GET",
url: "register",
data: $("#formsignup").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data);
}
});
return false; // avoid to execute the actual submit of the form.
});

Trying to set variable as part of URL in jquery ajax post

So I am trying to do a post using jQuery.ajax instead of an html form. Here is my code:
$.ajax({
type: 'POST', // GET is available if we prefer
url: '/groups/dissolve/$org_ID',
data: data,
success: function(data){
$('#data_box').html(data);
}
});
Here is my problem:
When this was in an html form, the $org_ID that was part of the URL would actually pull the variable and send it as part of the URL. Now that this is in jquery, its just sending $org_ID as text. How can I get this to figure out what the variable, $org_ID is? I tried declaring it in the javascript but I am brand new to jquery/javascript and don't really know what i'm doing.
Thanks!
Are you rendering this in PHP? In that case you need to do:
url: '/groups/dissolve/<?php print $org_ID; ?>'
Otherwise, you need to do something like
var org_id = 'foo';
// or
var org_id = '<?php print $org_id ?>';
$.ajax({
type: 'POST', // GET is available if we prefer
url: '/groups/dissolve/'+org_ID,
data: data,
success: function(data){
$('#data_box').html(data);
}
});
Unlike PHP, you can't interpolate variables in javascript, you have to concatenate them with the string.
If you're trying to POST a variable (org_id) then you should put it in data:
data['org_id'] = org_id;
$.ajax({
type: 'POST', // GET is available if we prefer
url: '/groups/dissolve/',
data: data,
success: function(data){
$('#data_box').html(data);
}
});
While you can concatenate params onto your url to send them in an HTTP request, putting them in a data object not only lets jQuery do more work for you & escape HTML entities etc (and keep your code cleaner), but also allows you to easily debug and play around with ajax() settings.
It's not clear in the question where your data comes from, but you can use something like:
url: '/groups/dissolve/'+orgId,
or:
url: '/groups/dissolve/?orgId='+orgId,
Short answer, concatinate
url: '/groups/dissolve/' + $org_ID

Categories