-3

If the combobox select the Archers the browser will echo Correct but if the combobox will not select the Archers the browser will echo, i want to know if my syntax is wrong.

Wrong. Error: Undefined index text_hname.

Here's the structure:

<form action="server.php" method="POST">
    <img src="images/logo.png" class="logo">
        <div class="container">
            <h1>Account Details</h1>
            <br>
            <label class="username">Username</label>
            <input type="text" name="text_username" class="text_user" placeholder="Enter username">
            <label class="password">Password</label>
            <input type="text" name="text_password" class="text_pass" placeholder="Enter password">
            <label class="email">Email</label>
            <input type="text" name="text_email" class="text_email" placeholder="Enter email">
            <label class="cname">Character Name</label>
            <input type="text" name="text_cname" class="text_cname" placeholder="Enter Character Name">
            <label class="character">Select Character</label>
            <select class="names" name="text_hname">
                <option>Archer</option>
                <option>Barbarian</option>
                <option>Balloon</option>
                <option>Witch</option>
                <option>Spirit</option>
                <option>Hog Rider</option>
                <option>Minion</option>
            </select>
            <img src="images/">
            <br>
            <input type="submit" class="submit" name="submit">


        </div>
    </form>


//option
$hero = $_POST['text_hname'];

if (isset($_POST['text_hname'])) {
    if ($hero == 'Archers') {
        echo "Correct";
    } else {
        echo "wrong";
    }
}

3 Answers3

1

The problem is that if you're trying to assign $hero before you've checked if text_hname is set, it might not be defined.

Suggested refactor:

if (isset($_POST['text_hname'])) {
    $hero = $_POST['text_hname'];
    if ($hero == 'Archers') {
        echo "Correct";
    }
    else
    {
        echo "wrong";
    }
}
HomerPlata
  • 1,687
  • 5
  • 22
  • 39
  • 1
    Surely it removes the error from your original unedited post though? Since you've edited it, the problem is clearly with your form not posting the value in the first place. – HomerPlata Sep 05 '17 at 17:05
  • how the browser will echo the if statement if I select the archers? I will use javascript or no? – Dave Spencer Sep 05 '17 at 17:06
  • I'm not in front of my laptop now, so can't sort out your form right now, but it needs some work to make sure you are actually posting a value from your select element. – HomerPlata Sep 05 '17 at 17:08
0

$_POST['text_hname'] will return the selected option value , not option name .

Modify your select like this:

<select class="names" name="text_hname">
    <option value="Archer">Archer</option>
    <option value="Barbarian">Barbarian</option>
    <option value="Balloon">Balloon</option>
    <option value="Witch">Witch</option>
    <option value="Spirit">Spirit</option>
    <option value="Hog_Rider">Hog Rider</option>
    <option value="Minion">Minion</option>
</select>

and server.php page

if (isset($_POST['text_hname'])) {

    $hero = trim($_POST['text_hname']); //** to remove any whitespace
    if ($hero == 'Archer') {
        echo "Correct";
    } else {
        echo "wrong";
    }
}

EDIT : it might be because your $_POST['text_hname'] has whitespace. Try trim() on $_POST['text_hname']

Hope it's helpful.

Sreejith BS
  • 1,183
  • 1
  • 9
  • 18
  • Note that: "If [the value] attribute is omitted, the value is taken from the text content of the option element." -- [ – showdev Sep 05 '17 at 17:13
0
//Your select in form needs values. Same values that you are going to compare.
<select class="names" name="text_hname">
    <option value="Archer">Archer</option>
    <option value="Barbarian">Barbarian</option>
    <option value="Balloon">Balloon</option>
    <option value="Witch">Witch</option>
    <option value="Spirit">Spirit</option>
    <option value="HogRider">Hog Rider</option>
    <option value="Minion">Minion</option>
</select>

//Php code
if (isset($_POST['text_hname'])) {
    $hero = $_POST['text_hname'];
    if ($hero == 'Archer') { //Its Archer, not Archers
        echo "Correct";
    }
    else
    {
        echo "wrong";
    }
}
Anis Alibegić
  • 2,941
  • 3
  • 13
  • 28
  • 1
    Note that: "If [the value] attribute is omitted, the value is taken from the text content of the option element." -- [ – showdev Sep 05 '17 at 17:15