So I'm trying to understand jQuery's .ajax function so that when I create my sites CMS I can update content on the backend asynchronously. I'm creating a simple form to start out. I pass the id of the database using php in the action href of the form. I pull the data from the form and convert it to json using the .serialize() jQuery function and then pass the action and the data into the .ajax function. On my the backend I use php to pull the $_POST and $_GET items and update the database accordingly. Everything works except that the site actually links to my backend php site... I just figure this out actually but I can post the answer for future references for people. Let me know if my answer is incorrect.
It looks like I was using type="submit" for the update input for the form. Because I was using action = "mybackendhref" in the form This caused it to link to the actually page that I wanted to use .ajax to push the data to instead. Changing to input type="button" caused everything to work smoothly.
*By "mybackendhref" I mean the actual site I want to push the data to using .ajax.
Related
I am fairly new to using PHP so bear with me if this is a stupid question
I have a form that comprises a number of radio buttons, the action is set to redirect to the same page and the method is GET.
A click on a radio button gets data from the database. The data is used to redisplay the same page with changed content.
The page URL has PHP arguments in it like the example below
localhost/basesite/mypage.php?itemID=8&name=city&number=9
When I access the page and click on a radio button I get a page with “no arg” because the URL reads
localhost/basesite/mypage.php?number=6
Two of the arguments are missing and that the last one is incorrect.
With no change whatsoever to the code except using ”post’ instead of “get” the whole thing works flawlessly.
I have used
form action= "" method=“get”
form action= “#” method=“get”
and many other actions using $_SERVER["REQUEST_URI”], $_SERVER['QUERY_STRING'] etc and combinations thereof.
Those that worked with POST did not work with GET.
I do not need to use POST as data is not written only retrieved from the database so I have no worry about data being written more than once.
If I have to I will use POST but if the user refreshes or uses the back button then the usual warnings will be issued by the browser.
What am I missing?
you should you use $.get which is a jquery method.
First, you should share your full source code for better understanding your problem. And also you have to use post method to submit a radio button values to get some value from your database. Form data can be submitted using these two methods (get and post). Both are used for the same purpose, but stands apart under some specifications. As in GET method key values are passed in the Url while in POST, the information transfers in a hidden manner.
Sorry folks. It was a badly formed URL due to me not fully understanding how to set a hidden element.
Is it possible to update the value of a variable from another page using JavaScript?
or
Is it possible to do this using form on submit, just update the other page, but remain on the page on which the form is located?
I think what you are searching for is AJAX (Asynchronous JavaScript and XML) which performs a get or post action and waiting for the result of the server. This makes it possible to submit a form without jumping to the page.
Have a look at this
Or if you use JQuery this
You can simply send all the data to be updated via Ajax and simply get the response on the current page You are in.
I read on the web many things about creating form in jquery mobile and I didn't understand one thing. When I create a form and I would pass data through php, I hear jquery mobile serialize them and automatically pass over and if the form has been created using post method I can use them in an other page only using _POST array and nothing all. But I read also some people like it form with ajax use ajax to pass variable. So I didn't understand why use ajax?
The default for the jQuery Mobile framework is to submit forms via HTTP POST. Why? because then your web application has greater control over the UI when animating through pages, creating a smooth transition. Well, a better description can be found in the jqm docs here.
If you are looking to disable the default Ajax functionality, just append this to your form element:
data-ajax="false"
Hope this helps!
I have a php page with some functions to recieve POST data, with a page refresh, no AJAX.
Right now I have a form with hidden fields that contain my dynamic data that I send with JS like so: document.my_form.submit();
Is it possible to send the data without using a form?
Basically I want to send an array of URL's from a list with thumbnails, so my function loops over the list and pushes them in arrays.
2000 characters should be ok these days:
http://www.boutell.com/newfaq/misc/urllength.html
Nope, you can only POST data via AJAX or a form.
you can use get instead of post, but it sounds like the url will get pretty messy. if you aren't opposed to ajax, i would be happy to elaborate on the answer.
So I am using "load" functions of jQuery to use ajax on my pages.
Can anyone write a small snippet of code using jquery load (or any ajax function which you think is the best) ? What I do is make a form, onsubmit pass to js, further using GET pass values into the load function and return false; though this works I am sure this is not the best way. Also this does NOT work in Opera.
It sounds like what you're trying to do is submit a form to an existing server-side script using AJAX. If so, consider using the jQuery form plugin. It will let you very easily make an existing form submit via AJAX.
If that's not your goal, we'll need a lot more detail about what — specifically — you're trying to accomplish.