Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP (Newbie in php) How can I print the "text" of a select option (form)? (help)

Marina_Eq

New Coder
Hello, new here, english is not my native lenguage but i'm gona try to explain what I need to learn. Sorry in advance, I'm learning on my own and may make a lot of mistakes

The problem is this: I have 3 documents:

- Form.php -> here is the form (first photo)

<form action="index.php?seccion=print" method="POST" enctype="multipart/form-data">
<select name="item" id="item">
<?php
include_once 'item.php';
foreach ($options as $value => $option) :
echo "<option value=\"$value\">$option</option>";
endforeach;
?>
</select>



- Item.php -> here are the select options inside an array that I linked to the other 2 .php documents

<?php
$options = [
'H' => 'option 1',
'O' => 'option 2',
'E' => 'option 3',
];
?>



- Print.php -> I want the results to print her

<?php
include_once 'item.php';


<!!!!---don't know what code to put here ---!>
?>


I want the option(item) selected in document form.php to be printed in print.php, but i only get the string and the array position printed when I tried different things.
Am I explaining myself? Can anyone help me?

Thanks in advance,
hope I have not broken any rules of the forum.
 
Try this:

PHP:
if(isset($_POST["item"])) {
    echo "The Result Is: " . $options[$_POST["item"]];
}

The value submitted by the form is the key to the array, so you have to access the key of the array to display the value.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom