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.
Related
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.
store value of input type="hidden" into a php variable.
The value is set by javascript into a hidden variable[input].
Now i need to save its value into a $myphpvariable.
I cant set the value into session with javascript and cookies is not an option.
After the page is submitted, hidden input elements will show up in the $_POST superglobal, just like any other form element. If the hidden form field were called coolsecret then the value would be in $_POST['coolsecret'] (or $_GET['coolsecret'] if you used method=get).
Before the page is submitted, there's no way to get a value from javascript into PHP, because your PHP code is all executed on the server, and the javascript isn't executed until the user has loaded the page on their computer. You should think about what you're trying to accomplish, and whether there's any way to compute the variable you want in PHP instead. If it's absolutely impossible to compute the variable ahead of time, then like Another Code said, you'll need to use something like an AJAX request.
What do you mean by save? When you submit that form you will be having value of hidden variable in $_POST or $_GET depending on the methodof form
You store php variable in js.but u cant stored js variable into php variable .because php run at server side and javescript at client side
The only ways to communicate Javascript results to a server-side script are:
Submit a form to the PHP script
Perform an asynchronous HTTP request with Javascript's AJAX functionality
You can't just mix the two, since Javascript operates inside the browser and PHP executes before a page is even sent to the browser.
Post it to the server then use $_POST["the_id"] to get its value.
Goal: I want to get the text from the selected element and then create a PHP variable from it.
I know how to do this using jQuery but can't wrap my mind around the idea of how to do it in PHP. Basically lets say I have a table with some data. When the user clicks on the data I would like to retrieve that text and then place it in a PHP variable. I could use .text(); from jQuery to create the variable but I could not pass that variable into PHP for further use.
I am very new to PHP (a front-end developer trying to learn back-end). So any explanation would be helpful also.
thanks!
***OK I now understand how to do it. Thanks for the input. I was not thinking. It makes perfect sense to create the variable using a _post and then returning the data via AJAX. Sorry for wasting your time. Thanks for replying.
There is no onclick event in the backend, at least not that works like in javascript
if you want to pass a variable to the server you can use a form and send the data through a GET or POST
Example:
<form action="index.php" method="get">
<input type="button" value="somevalue" name="action" />
</form>
when the user clicks you will see this in the url
www.example.com/index.php?action=somevalue
now you can retrieve this value in php using the $_GET global variable
$variable = $_GET['action'];
The same can be done using POST, this way the variable is not shown in the url
In order to have PHP gather any client side data you would have to pass that data back to the server.
To assign data from client action to a PHP variable, you would first have to capture that data using a front end solution (like your jQuery one above) then pass that information to your server application for processing using AJAX or form submission.
Your PHP data handler could then parse that information out from the data submission and push that info into a variable.
Your front end would look something like
$.ajax({
type: "POST",
url: 'your_file.php',
data: 'selected='+ captured_data,
});
and on your back end...
<?php
...some php code...
$var_to_hold_selected_thing = $_POST["selected"];
...more php code
?>
Specifics of how to do more than that would depend on your application...
You can do it with ajax:
$.ajax({
type: "POST",
url: 'your_file.php',
data: 'text='+ text,
success: function(data) {
alert(data);
}
});
After the .click() event you could have a dialog box appear, with Is this your text?. Then post it.
You could grab that text with jQuery using $(elem).text() as you've described and then pass the captured value to PHP using an AJAX request with jQuery (see $.ajax). The PHP script you pinged with AJAX (either via GET or POST) can then take the value you passed and push it into a $_SESSION variable.
session_start();
$_SESSION["text_clicked"] = $_GET["text_string"];
exit;
After you do this, any subsequent ajax calls made with jQuery (to any PHP script), can recall this variable, provide that you've properly initiated the session.
Stack 101's answer is the correct execution that you're looking for, however it sounds like you could use a little more background on the context first.
Basically, PHP is a server-side scripting language, which means that it gets executed before the page loads into your browser. Here it can look at a database, connect to other sites, or do a whole host of other stuff. Once it's in the browser, your HTML + CSS + jQuery is in charge of displaying + styling + and manipulating that information respectively.
AJAX is usually used to describe the act of posting data to a PHP script, and then re-loading it back into your page without the page refreshing. In other words, jQuery passes data to a PHP script that runs on the server, and then loads in whatever the PHP script outputted. Sometimes PHP can output an entire site (html tags, css, js, whatever). Other times it can simply access a few variables that it got from the POST array, and output a result. This is much more common with AJAX requests.
A quick search turned up this tutorial which might help (I haven't followed it)
http://www.php4every1.com/tutorials/jquery-ajax-tutorial/
Good luck!
I believe it could be done by sending all the required content through GET or POST, and then make PHP stuff it to a variable, however I'm not exactly sure.
PHP runs on the server. Variables come and go each request. Everytime you refresh a page (or send a request otherwise), a PHP script starts running, spits out a response, and stops again. So setting just a loose variable is not really an option.
The options you got are
- Use a cookie. You can set it from Javascript and it will automatically be sent with the next request. Your PHP script can read this cookie.
- Send the variable by doing an AJAX (asynchronous) request. You can send the value in the url or as posted data.
In either case, you can decide to use the variable only once, or store it in a session. I think the cookie will remain unless you explicitly clear it. So you could keep using that.
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.
i am using a dropdown in php which asks for user_category before creating the user. Upon selection of the category further text boxes are to be created dynamically depending upon the selection. I know how to get the selected value from $_POST but here i need it befor form submission.Pls help
You would need to use js. Either:
document.getElementById('elementName').value
or
document.formName.elementName.value
or
use jQuery.
Than if you need a response from a php script before submission you would need to use ajax.
You cannot do that ONLY with PHP. PHP is a server-side language, so once you request the page and the PHP interpreter on the server interpreted it and served you the HTML, it does not know anything about what happens to the client. So PHP cannot know if the client changed selection.
What you can do is use JavaScript to do that.
You have 2 options:
1) have the text boxes values that you want to show hard-coded in Javascript for all the cases
2) query a PHP file asynchronously using AJAX so that it will return only the needed values.
You can not get them in php before submission; you may also consider something like simple html dom. Or you can get them with javascript also with something like document.getElementById
you can make this in steps like this: so after the user clicks the submit button right after the dropdown. now you have users choice in the $_POST and you can display the appropriate form. or you do it with js, like nico sad.