Getting selected text - php

I need to get a text that the user has select with pointer and put it to some variable in PHP. So, the user should select some text in one div, then click submit button (in another div), and then I need to store that part of the text to some variable. Can anybody help me?

You would have to set an onclick listener (I prefer to use JQuery for this) so that when the user clicks the submit button, you grab the selected text from the div. You could probably do that in a way found here:
Get the Highlighted/Selected text
Since javascript and HTML are front-end and PHP is back-end, you cannot simply place the selected value into a PHP variable. Instead, you can use an ajax request to send the data to a PHP function. I'm not sure what you are doing with it after that, but sending an ajax request and including the selected text in the data section of the request, and then retrieving it in your PHP file/function and storing it in a variable there. This question should help in that regard: Passing Javascript variable to PHP using Ajax

Related

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.

HTML form within another form

I am new to html, I would be really glad if you can help me with this.
I have a web page where there is a list and some other text inputs and buttons. This option list can be populated by clicking the "add" button in the page, this add button is to direct to another page and in that page there are some chekboxes, those which are checked are loaded back to the main page,(where I have the list) .
At the end data in the main page needs to be loaded to the database, (what is in the list and in the text inputs).
Since I'm new I just know little about php and html, I thought I should have a form within a another form(form to "add items", form to load to the database) and it is not possible in html. Can anyone suggest the best way to do this? Do I need to use javascript?
Why can't the extra inputs (the ones that would be in the second form) be part of the first form? I think the question will become clearer if you post a sample form so we can see the relationship between the two forms.
But overall, since you're ultimately only submitting one form, then maybe all the inputs belong together. If what you're calling the second form isn't supposed to be visible right away, you can still have it be part of the same form, but only reveal it when needed.
Again, some sample data would help to understand the exact context of your question.
in php if you use input name="somename[]"
for a number of input elems
you will get an array in $_POST['somename'] and access all the values.
I think what you're after - if I understand you correctly - is ajax. Ajax allows you to asynchronously send data to/from another script without leaving the current page. This is accomplished using JavaScript. In your case I think what you need to do is set an onclick event in JavaScript to a button:
<input type="button" onclick="javascriptFunction()">
You can read more about ajax here:
http://www.tizag.com/ajaxTutorial/ajaxform.php

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.

using the selected value of a drop down before form submission

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.

Categories