php -results should come in same page - php

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

Related

Form fill Submit/Redirecting, but not Refreshing (current page), without jQuery or Ajax, in PHP

Is it possible to fill a form and Submit & Redirecting, but without refreshing the current page; without using jQuery or Ajax, in PHP.
I know how to do it by using jQuery, but the thing I was wondering is that why does the page refresh happens (on current page) and how to submit/redirect without the happening of the page refresh. In short everything happening in html/php
Quick answer No
PHP is a serverside language and therefor cannot be used to trigger client side events without using some form of javascript/jquery and websockets (or keep polling with ajax to the php script).
But what you can do is submit the form and redirect back to the current page. (The page will be refreshed)

Submit an action without refreshing or redirecting the page [duplicate]

This question already has answers here:
jQuery Ajax POST example with PHP
(17 answers)
Closed 7 years ago.
So, I have a contact form on one page (let's call it send_now.php or example.com/send)
Once the form is filled out and submitted, then an email is sent to a certain user while the page is directed to example.com/it_is_sent page which contains a sent confirmation based on confirmation.php.
I would like to know how to change it so that everything is done on example.com/send/ page without refreshing or redirecting the user to the next page.
Here is what I mean.
So, in /send/ page, an user fills out the form and click send. Then without redirect the user to /confirmation/ page, the confirmation is shown on /send/ page without redirecting the user, so everything happens within the same page.
Is there a way to do that? what is the general concept of doing things like that?
or, can the form be submitted within the same page without refreshing the page?
Thanks!
Take a quick search around the net for "jquery ajax form submit". The term I think you're looking for is Ajax. It is what allows you to have JavaScript send off data to a PHP script without refreshing the page.
You build your form like normal, and attach a jQuery click event to the form or submit button. The jQuery/Ajax function takes the data from the form and sends it over GET or POST to your PHP form.
Whatever your PHP script outputs is received by your jQuery/Ajax function. I like to use json_encode on a PHP Array for the PHP script output. In JavaScript I can then easily work with the results as an array of values.
Depending on what's in the Array or output depends on how your JavaScript should react. Output could be as simple as a 0 or 1, true or false, or a json Array or values like I usually do. I'll usually include at least error=true/false.
You could have the PHP script output be displayed in a Div once the Ajax success function fires.
You could also use jQuery load() to load another page into a Div upon success. The possibilities are endless when you combine it all.
You can easily find code samples for this all over StackOverflow and tutorials on the rest of the Internet. You're looking for "jQuery Ajax Form Submit to PHP", maybe even with MySQL?
This technique makes buttons that make instant changes possible. Once you're done with this project, look into websockets if you really want to see how instant the web can be.

Form submission with Ajax and jquery

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.

PHP MYSQL Updatable Form

I am trying to create a form that you can enter data into mysql using php and then it will update the table on the same page instantly here is what I have
Index_test.php: http://pastebin.com/03fndSHG
Update.php: http://pastebin.com/jQraSskS
index_style.css: (http://)pastebin.com/PhYxttFu
When I submit this I get a double entry in my form and I want the update to be seamless like on the index_test.php it shows a line that says "An entry has been added" and the table refreshes automatically without any movement to another page. I ahve tried finding something with Ajax but nothing I try works
If I were doing this, I would use jQuery for the ajax. There's lots of documentation here: http://docs.jquery.com/Main_Page. It simplifies ajax, if you know how jQuery works. To use jQuery, you'll need to know some javascript as well. Without jQuery, just javascript & php is enough, but trickier because internet explorer does ajax differently than the other browsers.
Without ajax, you should probably submit the form to the same page as the form, which then redraws itself with the new table row. Even with ajax, if the user turns off javascript, then the form needs to submit to the same page, or another page that has the form in it. This is known as progressive enhancement or graceful degradation, meaning, the web page still works if javascript is disabled.

Can I have PHP redirect to an in-page link when I process a form?

I have a vertical scrolling website (lots of in-page links). I also have a contact form script I'm working on.
I'm trying to set it up so when someone completes/submits the contact form, it redirects them to #contact_area (on the same page), but calling the header function after is throwing a "Cannot modify header information" error.
Any suggestions on how to redirect after a script is processed from within ?
Thanks!
A header redirect needs to happen before PHP prints any output. If you want to direct the user to an anchor on the current page you have two options:
Submit the form as normal. Your PHP script processes the data and does this before any output: header("Location: /my_same_page#contact_area"); The page will be reloaded but they'll end up in the right spot.
Submit the form data via AJAX and then scroll to the #contact_area anchor.
The second options is probably the cleanest but the first one should be a lot easier for you to implement.
You can work around this using output buffering, few examples here
I am assuming you are posting the form back to the same right?
Maybe submit the contact form to another page for example contactus.php and once its been successful place your header location code into that OR you could use the jquery form plugin (submit() function I have used) which runs in the background and then you could maybe jquery scrollTo() your hashtag.
Go got option 1 and if you have time then play with the jquery version maybe.

Categories