I currently have a PHP page in which there a couple of div elements and a listbox. Also, im getting certain data from a database using PHP. I want to be able to modify the text area of the div elements based on the list box selection using PHP. Any ideas on how to do that ?
-
Oh, and i'd prefer not using AJAX.Cygnus– Cygnus2012-06-07 07:13:43 +00:00Commented Jun 7, 2012 at 7:13
-
1Cant you just iterate the database results and add them in the output before you flush it? Can you show us some code?MrKiane– MrKiane2012-06-07 07:15:13 +00:00Commented Jun 7, 2012 at 7:15
-
switch statement can be useful. u can echo any div depending on the which option u select from the lists3polz– s3polz2012-06-07 07:15:58 +00:00Commented Jun 7, 2012 at 7:15
-
But how do i get current listbox selection using php ? And the data has to be also modified using PHP after the page has loaded and all.Cygnus– Cygnus2012-06-07 07:22:21 +00:00Commented Jun 7, 2012 at 7:22
-
If you're using only PHP, the page must be reloaded in order for the data to be updated, as PHP is a server-side language. If you want it to be modified without reloading the page, you'll have to use something like AJAXMysticXG– MysticXG2012-06-07 07:25:03 +00:00Commented Jun 7, 2012 at 7:25
3 Answers
With PHP alone you can't, because it is a server-side script and so when the page reaches the client it cannot be edited via php.
You can load every possible div into the client page and hide all except one (the default one) and then show/hide the appropriate div based on the list box selection, but you do need JavaScript (or similar) language for this
PHP: Server-side script (no access to client input except from form submissions!)
JavaScript: Client-side script (can react to user input and events)
10 Comments
Make sure the listbox (presumably a <select multiple>) is in a form.
Submit the form to your PHP program.
Output different content from that program between <textarea> and </textarea> (or <div> and </div>, you question is a bit unclear as to what elements are actually involved).
1 Comment
You can give an option to submit the form when the <select> tag focus or content changes. This can be done by:
<form id="numform" method="post">
<select name="number" onchange="numform.submit();">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</form>
And based on the $_POST value / count, populate the value in the div.
<div><?php echo $_POST["number"]; ?></div>
Do let us know if this helped. :)