Is it possible to call a PHP function from js file? [duplicate] - php

This question already has answers here:
Call php function from JavaScript
(5 answers)
Closed 5 years ago.
Is there any way I can call a PHP function from jQuery? I have a .js file and a .php file.
I have a jQuery event applied on a button click. Now I want to set a condition on what happens next when the button is clicked depending on whether a user is logged in or not. Is there any way I can do this from my .js file ?

You can call any php file by the common HTTP methods, e.g. GET and POST.
What you might be looking for is AJAX. Google the term, and also look into jQuery for a simplified approach.

You can send it to a php file with ajax and use your php file to check if the user's logged in. Then you can send a response back to your js file to see which event you should trigger

Technically, yes. But why would you want to in this case? My suggestion is to look into storing values in cookies, since both PHP and JS will have access to them.

Related

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.

Redirecting user to a webpage with PHP [duplicate]

This question already has answers here:
How do I make a redirect in PHP?
(34 answers)
Closed 8 years ago.
So I have an HTML webpage with a form on it. The form sends its data to a PHP file on the server.
After processing the form data with the PHP file, I want to redirect the user to another HTML webpage.
How would I do this?
This is usually done by PHP header function, like this:
header("Location: http://your-domain.com/your-page.php");
You can read more about it in PHP manual or from here.
Though this question is a duplicate. I'll answer it. You do it as follows:
header("Location: http://yourwebsite.com/otherpage.php");
In PHP you have to use below code snippet to redirect from one location to the other.
header("Location: page.php");

Weird $_SESSION behaviour in PHP [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to make php scripts run in parallel?
I'm currently having a (in my opinion) weird problem with the $_SESSION superglobal in PHP.
On page unload ($(window).unload) jQuery sends a synchronous post request to save.php.
It contains some data that should be saved in $_SESSION["data"]. I can fetch the jqXHR object and display some test strings contained in responseText with alert() when the current page is still shown. But on the next page those data is not available, yet. I just have to reload and everything is fine.
It seems to me that my browser (Firefox/Chrome) load the next page in background while the post request is not finished yet. Or is it a problem with $_SESSION?
May be attach event to window.unload is not a good idea. Browser may fetch the next page first then call the event, In that case you seesion is not change (yet). So instead of attach to unload event, You may attach to the event that cause navigation
For example:
Attach to a link click, cancel default behavior, call your ajax save.php script then manual do navigation using window.location.href
Attach to form post, cancel the post, call ajax then post form....
Hope that help

Upload file with php and ajax without using the usual form post method

i'm trying to upload a file with php. The usual process is:
form POST->php
But i'm using this method:
form->button with onclick event that calls ajax/javascript function->php script call from javascript
I'm passing the full form (this.form) to javascript, javascript do some stuff and send all the form inputs one by one to php using GET (xxx.php?xxx=xxx&yyy=yyy....), one of those inputs is a FILE , and I don't know how to send it to the PHP script trough javascript function.
Thanks in advance
First of all you can't send a file by GET, just use POST, second you need a flash uploader for < IE 8 and for other browser you need an iframe. There is no other solution that i am aware of.
You can use HTML 5 File API to upload file via JS (here is nice tutorial). But you must be cautious, because server may have max URI length limit (see this for more detail).

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

Categories