Form submission with Ajax and jquery - php

I have a PHP file that has an HTML form that submits via AJAX to the database. When I hit the form submit button, every PHP query updates itself. Is this how Ajax normally operates? or if I switch the parent file from PHP to HTML, will it eliminate the unwanted updating of all the PHP on the page?

jQuery ajax submits the request to a seperate php file to load data. You can choose to load only poritions of pages. $.load does this easier. Look here: http://api.jquery.com/load/

You said it submits via AJAX to the database - but have you actually programmed it to do that?
Post a sample of the AJAX code and we can check it for you.
I have a feeling you don't really understand the technology you are using. What is a 'PHP query'? You want to switch the page from PHP to HTML? Well, can you? Does the page have PHP in it or not? It wouldn't make any difference to AJAX code, but I have a feeling you don't actually have AJAX code.

Related

Why is Sending Data with JQuery Necessary?

What are the advantages of using jQuery Ajax to POST data to the backend?
Is it necessary to use jQuery Ajax to POST if your html and php are on the same page? (meaning it only reloads, not redirect, after being submitted traditionally).
Whether to use jQuery or vanilla JavaScript is your choice. Ajax is useful if you just want to send a small size of data to the server without downloading the entire page from the server.
Suppose you have a page with 1000s of lines of HTML and you need to submit a simple yes or no response to a question to the server. Now, if you just redirect to the same page, it will still download most of the HTML page again. Ajax will just send that bit of data you want to send to the server and get a response which you can use within the client side to make changes to the page.

Calling PHP function after jquery code

Dear Firends I have large number of forms on a single web page all of them calls a single PHP function. However what I want is that the forms should call a jquery function and if there is a need then jquery should let it call the PHP function.
I do not want to use Ajax just want to create a PHP function call if the matter can not be solved by jquery.
Each of the form is associated with some data. how ever all the data that is displayed on the page is not available all the time. So what I want is
if (data == available) { call PHP}
elseif (data != available) { jquery alert('sory bro');}
if data can not be seen now just use jquery to say sorry (no need to check from server). When a page is loaded we know which all pieces of data can not bee seen and are given in different color.
The forms are generated using a PHP loop with each form showing different data but of same type (each form is assocaited with a sort of Article).
All the questions that I have seen are about Ajax. Where as my current PHP code is working fine. all I want it that before making a trip to server if the data is not available the jquery shoould say so. We already know which data is not avaiable so far.
I hope I have explained it
Thanks a lot
**I think I have not made my point clear.. When the page is loaded is already know which data is not available for display and it is marked in seperate color and the div has different arrtibute...*is there some way so that I do not call PHP function for those forms?
PHP executes on the server side. Javascript (jQuery) executes on the client-side. So PHP is completely done executing before Javascript starts executing.
That's why everyone is saying you need to use AJAX. AJAX is a way to make a call back to the server in order to execute PHP code. PHP code only executes on the server. So in order to execute PHP, you have to make a call back to the server.
According to your logic, the data is present on the server.
If you want to know if the data is available or not then you have to contact the server right.
If that's the case how can it be solved without sending an ajax request..
You need to make the request as jQuery is a client side code and cannot contact the server directly.. You need the server side script to execute it which is PHP in your case
The easiest solution:
if (data == available) { $("form").trigger('submit') }
elseif (data != available) { jquery alert('sory bro');}
Obviously you need to adapt the selector according to your specific form / requirements.
Just make sure your form does not get submitted accidentally when you press submit by adding something like event.preventDefault(); in your function.

JSPX form submits to PHP

Is it possible to have a JSP/JSPX form to submit to a PHP file?
The PHP file will then be in charge of validation and database update. After that it will send a sort of response to the same JSP/JSPX form. Also this should be done in AJAX using jQuery.
How can I do that and what are the underlying concepts needed?
Thanks!
You don't need anything specific in your case. Just submit the form via Ajax using jQuery to the PHP page and process the result in JavaScript as well. All the code necessary for that will be client side in JavaScript and operate on the plain HTML generated by the JSP page. The only restriction/problem might be if these two pages run on different domains.

Submitting form without refresh

I am retrieving Google's weather API XML and using PHP. I'm retrieving the weather for any searched city.
Now, this "app" is under a tab and whenever I submit the form it refreshes and I want to prevent this.
Is it possible? This will be implemented in a dashboard - thats the reason I want to prevent the refresh.
This is what I mean: http://www.screenr.com/30As
The entire code is here: http://jsfiddle.net/ZshpF/
Simply, copy paste the code in a php file and it will work.
You'll want to use AJAX. Since you mention jQuery in the tags, it has a very handy function for making the call to the server. But making the call from the JavaScript is only half the story, you'll also need something on the server listening for that call. It would essentially be another PHP script which acts as a page in and of itself, but would return data in the form of (most likely) JSON instead of HTML. It's not meant to be human-readable, but rather to be a sort of web service for your JavaScript code to use.
You can find a simple example here.
I think you can use jQuery ajaxForm

Run PHP from JavaScript form submit

Can anyone explain how to run PHP code from within javaScript for submit?
Currently I submit the form to a page.php, connect to MySQL, run some queries then build up the HTML and echo it out.
However, I want to do this without going to the page.php as I am trying to show the results in an ajax dialog using JQuery-UI. It works, just I have to submit to page.php to make it happen.
Would I do something like build up the code in Javascript writelin statements and dynamically load a div or something?
Use jQuery's post() method to send the request to page.php from JavaScript asynchronously. You can use the success option to show the dialog http://api.jquery.com/jQuery.post/

Categories