0

I am relatively new to PHP. I am taking a class and working on a form. So please keep that (me being new and learning) in mind if you decide to help. 8-)

Here is the deal. I have a form. I am using a drop down for the states. I have created a variable to echo out after the form has been submitted. The challenge is that the variable does not print. I am not sure how to get this to work. What I think I need if for the 'value' to get assigned to the variable.

I did search for a solution via google but I do not think I am using the appropriate search terms to find what I am looking for. For this form we are not using a database yet.

<select>
    <option id="state" name="state" value="AL">Alabama</option>
    <option id="state" name="state" value="AK">Alaska</option>
    <option id="state" name="state" value="AZ">Arizona</option>
    <option id="state" name="state" value="AR">Arkansas</option>
    <option id="state" name="state" value="CA">California</option>
    <option id="state" name="state" value="CO">Colorado</option>
    <option id="state" name="state" value="CT">Connecticut</option>
    <option id="state" name="state" value="DE">Delaware</option>
</select>

Now the variable is assigned here:

 $state=$_POST['state'];

And then I have the echo here:

  echo '<li>State: ' . $state . '</li>';

When it prints out it just prints the State:

Can anyone help me understand why this is not working and how I can resolve it?

Thank you in advance for your help. Please let me know if you need any more info from me.

1
  • A valid HTML document will only contain unique ids occurring only once. You have MANY options with the id of state. Beyond that, it is the select field that is read by the submission handler -- give the select tag a name. Commented Feb 12, 2024 at 5:46

2 Answers 2

2
<select name="state">
    <option value="AL">Alabama</option>

Change please.

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

1 Comment

id should go on the <select> as well, as it should be unique within the document.
2

Firstly, only <select> bears the name attribute, not <option>, remove those from them all.

  • <select name = "state"> bears the name attribute.
  • <option value = "AL"> bears the value.

You also did not post the rest of your form, therefore it's unsure if that is correct.

Make sure it contains a POST method.

I.e.: <form action = "whatever_action_is" method = "post">...</form>, <select>...</select> belonging inside the form tags.

  • whatever_action_is, that could be action="" as self or action="handler.php", being another file or the same one you're using; that is unknown.

If you are running both your HTML and PHP inside the same file, use a conditional statement.

I.e.:

if(isset($_POST['state'])){
   $state=$_POST['state'];
}

else{
   echo "Not set.";
}
  • Otherwise, you may receive undefined index notices.

  • id="state" ID's are unique, therefore you either need to give them different ID's, or use a class if you are using this with CSS. class="state"

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


HTML stickler.

<li></li> requires the <ul></ul> unordered list tags or <ol></ol> etc.

Depending on which type you want to use.

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.