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/
Related
I have a web page generated with php with a form placed within a div. The form is submitted using Ajax and validated on the server side. If there is an error, the contents of the div is replaced with an updated form with error messages. If the form has no errors in it, I want to reload the entire page from the ajax response, not just the div.
Does anyone know how to do that?
I have tried sending location header but it places the new page in the div. I have also tried javascript window.location but I don't know hot to get it executed when the ajax response has been received. I am not using jQuery so I want to do this in plan javascript and/or php.
Thanks in advance!
Yes, you can do that in your callback as follows:
location.reload(true);
Honestly, that kind of defeats the purpose of doing something asynchronously.
I have written a login form, in this I have used, form and method is post and action is(PHP self) <?php $_PHP_SELF ?> (am using php in the form itself). In php i have written if the login is success then 'welcome' and if it fails then 'sorry'.
But those results are coming in a new page. I want to get those results in the same page when i click the submit button. How can I do this?
PHP is a server-side language, so after submitting the login data, a new page is generated by the server, if you want the welcome || sorry message to be returned without refreshing the page, you'll need to use ajax. I gather you're rather new to all this, so google some javascript Tuts on ajax, or use a JavaScript lib if you need to have this up and running fast. The easiest imho is jQuery, as it is the most documented of the lot
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.
I have a php function that I like to run in my code when a particular image is clicked. Currently I'm using an onclick function in javascript but it seems like the function is run everytime I refresh the page as opposed to just when I click the image. Can you guys please help me understand this? Should I be running a AJAX script?
This is an "order of execution" problem: All PHP code is executed before any HTML is presented to the user. If you want PHP to execute based on a user action, you need to initiate a new request to the server (via AJAX or a standard link).
Why don't you just link the image to the PHP file that contains your function call?
If you want it without having to refresh the page, then you can make an asynchronous call via AJAX to the said PHP file, and perform the necessary functions.
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.