0

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 ?

6
  • Oh, and i'd prefer not using AJAX. Commented Jun 7, 2012 at 7:13
  • 1
    Cant you just iterate the database results and add them in the output before you flush it? Can you show us some code? Commented 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 list Commented 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. Commented 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 AJAX Commented Jun 7, 2012 at 7:25

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

10 Comments

OK, sorry for being unclear. I have a page named client_show.php. Now, within that page is CSS code and my div elements styled according the CSS. There's also included a php file named getdata.php that contains functions for retrieving data from MongoDB database. The listbox is populated using PHP. Now when the user changes his listbox selection, the content of the div elements must change accordingly, and this has to be done in PHP because the data to be changed is available only from PHP.
@Cygnus: you need to use forms and submit when the select box changes, check out my answer, if it helps.
You can't change dynamically on client side. read my answer carefully :) I understood what you asked for, but you can't in a single page. You need to pass through a form if you don't want to use my suggestion or using AJAX (which is the best solution and it isn't that hard)
@STTLCU : Yeah, i understood your answer. I'll have to look into AJAX then. But iid heard there's something called HTML DOM to do this stuff. Is that correct ?
Yes it is and it is definitely the way to go (you'll need that after you sent a request via JS AJAX and got a response). Anyway the HTML DOM is always manipulated client-side, so PHP cannot use it.
|
1

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

I've given a proper explanation in the comment to the answer below.
0

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.