Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

Get Text From <option> Tag Using PHP

I want to get text inside the <option> tags as well as its value.

Example

<select name="make">
<option value="5"> Text </option>
</select>

I used $_POST['make']; and I get the value 5 but I want to get both value and the text.

How can I do it using PHP?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In order to get both the label and the value using just PHP, you need to have both arguments as part of the value.

For example:

<select name="make">
    <option value="Text:5"> Text </option>
</select>

PHP Code

<?php
$parts = $_POST['make'];
$arr = explode(':', $parts);

print_r($arr);

Output:

Array(
  [0] => 'Text',
  [1] => 5
)

This is one way to do it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...