how to display value in dropdown after selecting it?

Good day phphelp :slight_smile:

just want to ask if there are anyway to display a value of a dropdown menu after selecting?

[php]
Bread
Pizza
Chicken
Spaghetti
Sushi
Potato
[/php]

for example after selecting any option, the paragraph below will be replaced by the value of the selected option on the same page

[php]

replace text here

[/php]

the Question is
惻is it possible only by PHP?(on the same page?)

I did some research and it might be possible by Ajax or Javascript
but I only want to use PHP.

Regards :slight_smile:

This could be accomplish by doing something like the following:

[php]echo (isset($mySelection)) ? ā€˜

’ . $mySelection . ā€˜

’ : ’

replace text here

’;[/php]

of course there would be more code in order to get it to work properly, but in short yes it could be done solely using PHP and on the same page. Though it would look cooler if some JavaScript (jQuery - my preference) / Ajax was thrown into the mix. :wink:

Thanks Mr.Strider64 for sharing some techniques,

I tried something like

[php]

option

Bread Pizza Chicken Spaghetti Sushi Potato <?php $mySelection=$_GET['food']; echo (isset($mySelection)) ? '

' . $mySelection . '

' : '

replace text here

'; ?> [/php]

is it really possible to change the value in real time the moment you clicked the options?

I’m not really sure if I have to use $GET or $POST method but it requires right?

isn’t PHP a server side script? so I’m not sure how it works on client side… :frowning:

it will work if I have to use the form like this

[php]

option

Bread Pizza Chicken Spaghetti Sushi Potato <?php

$mySelection=$_GET[ā€˜food’];
echo (isset($mySelection)) ? ā€˜

’ . $mySelection . ā€˜

’ : ’

replace text here

’;
?> [/php]

but I want the value to change the moment I clicked the options, any suggestion? :slight_smile:

bumping thread~ any suggestions? :frowning:

Hanchan,

You have a form on your HTML page. It contains a form ā€œfieldā€ in it. You alter this by using the SELECT
and drop it down to the option you want.

From there, you have two choices. 1) process it server-side. 2) process it client-side.

To handle it server-side, you should use PHP or ASP or any other server language to read the value
and post back a page to show the updated version.

To handle it client-side, you should use Javascript or JQuery to process the data dynamically.

Either way will work, but, this really depends what you really want to do with this data. If you are
planning on the creation of a full website that takes this selection and saves it inside a database,
then you should do the processing server-side as it is much more secure. If you are just playing around
with code functions or are just making a site that does not matter about being secure, do it on the
client-side.

You should not use method=ā€œgetā€ as it shows the data on the page and is easy to hack. You should
use the method=ā€œpostā€ version. For handlind this process on the client, you can just use a simple
Javascript to grab the data. For Client-Side Javascript, you assign an ID name to the SELECT. JS uses
ID’s not name’s.

If you explain what type of site you are creating, perhaps we can help you with the code further.

So, lots of ways and lots of programming tools can handle this simple process. Hope this helps…

Thanks Mr.ErnieAlex!

I was just playing around and just wanted to know what are the methods.

so I think I have to use both PHP and Javascript?

because I want the data to appear after selecting on one of the option(dynamically) then I also want to pass it to another page.

I also tried the ā€œpostā€ method on the code I showed but it does not seem to pass it on the same page. it’s blank, so I used ā€œgetā€ method.

so I guess I’m gonna have to study DOM then? :slight_smile:

Well, first, when you use POST method, you use POST method to get the data back.
You use $somevariable=$_POST[ā€œform-fieldā€]; To get the posted data back.
But, of course, this would be if you plan to send the data to a database or another page.

For ā€œDynamicā€ displays, use Javascript. You can use BOTH of these by using JQuery/AJAX to send
data on the page behind the scenes to a PHP file by using AJAX to dynamically load a PHP page
into a DIV. This works well to load new data using PHP from a database and display it on the
current screen page that has already been loaded into the browser. It does get complicated as
to how you handle your data sometimes, so you have to be careful how you design this type of
code. Also, security can be complex sometimes as using browser-side code gives it up to the user
as they can see how your JS is used. Of course, if it pulls from a PHP file, they only see the name
of the file. But, that is one more thing for a hacker to ā€œplayā€ with.

Lots to think about… Keep on learning…

Thanks EnrnieAlex

guess this is gonna take a while~ but it’s a challenge for me :slight_smile:

Well, we all like a good programming challenge…

I suggest first deciding which way to go. Either using PHP and posting or Dynamic JQuery…

Either will work, but, they work better if you use one version…

I would recommend using jQuery. I know little about it, though, I should learn it soon.

Well, again, I would say for learning, you should attempt BOTH ways.

A standard PHP posting system with HTML/forms and PHP code to handle programming.

A HTML/forms page with JQuery/AJAX pulling PHP pages from the server and dynamically updating the
data that they create.

Both ways are easy to do with the current technology on on sites and browsers. But, both have very
different ways of handling the data. And, both have different security issues. So, lots to learn on both
versions.

If I was starting this process, I would first design what the site would do and then program it with the
best version that fits the use of the site. That differs depending on the data, users and ā€œflowā€ of the site.

Lots to consider…

Sponsor our Newsletter | Privacy Policy | Terms of Service