jquery and php relations - php

how can i store jquery ajax result in a php variable?
i mean i want to get result of ajax jquery and store it in php variable and ECHO it later

Why do you want to do this?
What do you mean by "later"? Later, on the same page, or after the user navigates to another page?
If you want to echo it back to the page which has the ajax, then use js/jquery to echo it instead. If you want to save the variable in a session so that you can display it on another page, then you should be doing that on the php page you're presumably calling via ajax, rather than calling a php w/ ajax, having it return something, and then sending it back to the php again. However, if you really want to do that, you would have to stuff the variable into either a GET or POST variable so that it can be sent back to the server when the user navigates. i.e., put it into a hidden form and force the user to submit it, or put it into a link.... actually, you could do it with a 2nd ajax call, but now we're just going in circles aren't we?

why dont u use the data argument in the load function
syntax for load
$('selector').load(url,data,callback)
for data use {'key':'value'}
and then in php use $_REQUESTED

Related

How to update database with server-side information without reloading the page

I'm developing a like/dislike system using php and MySQL, but I have a problem. When I add a like, let's say, I do it by adding a row to the likes table, that contains the post id and the user id. The user id is stored in a $_SESSION, and I can't just pass it to the html and make a AJAX request to run some php code and add the like, because this way it's too easy to simply change the user id.
So basicaly I don't want to reload the page to first get the $_SESSION value but I don't now how to get it's value after the page has loaded. I don't know if this is doable with php only, if not, what language should I use
if you're using send ajax request for saving the like or dislike value. after saving the like/dislike value. you have to return a new like value count. in PHP simply echo the value that will automatically come to your ajax success if not simply return the new like/dislike info, encode with JSON and return the value.
get these values in the success function and target the like and dislike button or div and change the new value with an ajax response.
if you want to code answer please share your code here so it's better understood by everyone

Display form results on same page with action to a different page

I Have a form with the following in index.php:
action="calculate.php"
In calculate.php I do some math with data retrieved from index.php.
How can I display the results in some inputs in index.php ?
Ideally, you can use session set the value of the result in calculate.php then redirect to index.php from here, This is where you read the data, you could run some kind of check to see if the result is set in the session and then display it. Don't forget to unset the session result after displaying it.
This would work but It's not the best way to go forward. I would recommend that you do everything in a single file and use a post request to process and display the result, You can also move the calculation logic to functions which can be in a different file and invoke them as needed.
Why not use ajax? Use javascript/jquery to catch the submit event, use ajax to send the data to calculations.php, echo the results back.

Change php variable value with jquery

I have the following issue. I am building a form in which a user can select a value from a select option and i want to save that value into a php variable. The form and everything are in the same php file. Is there anyway to achieve something like that with jquery?
I do not want to submit the form, just to change the value of php variable.
Thanks
Your script is no longer running, when the user received the HTML-Form, since the request ended. Thus, there is no variable which you could set.
However, you can send a new request via jQuery.ajax() with the value of your select field. How to handle this request, depends strongly on what you want to do with the value the user provided.
I think you can not. You can make an ajax call to PHP, but this will load the same page again in the background. This will not use the php variable of the current loaded page.
Maybe you can provide us some sample code to have a better understanding of what you try to achieve.
No, it's not possible without submitting the form. You can manipulate any elements thus form elements' value on a page with JS via the DOM and when the form is submitted they become PHP variables on the server-side. PHP variables can only be altered on the server-side.
PHP is a server-side language while Javascript is a client-side language only. Unless you make an AJAX (jQuery version) call or a form submit, your Javascript will never communicate with PHP.

Update a value of a JavaScript variable from another page

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.

Sending JavaScript variable to PHP

I have to send a value that is stored in a JavaScript variable to a PHP page. The PHP page is in a different folder than the JavaScript page.
This JavaScript variable is in a method that fires when we click a button.
How can I send that variable value to the PHP page?
(This is an Eclipse project.)
The easiest way to do this is to use jQuery's POST method to send serialized data to a PHP page that can process your request.
However, you didn't say if you want the client to cache the value and always remind server of the value for all future requests. If so, you can once again use JavaScript to store values inside the cookie, so that every time user click on something (GET / POST) it will also send out the cookie containing your javascript value to the server
Using Ajax
Do you submit a HTML form when the button is clicked. If yes then you can set the javascript variable value into a hidden field of the form on button click. After which your PHP code and read its value on the server-side.

Categories