Domanda

I asked this question already but have since over hauled the code following the help I got. I am trying to make my php drop menu sticky but it clears to the top menu item every time after the submit button is pressed. I am not sure where I am going wrong so any help is greatly appreciated. Code as follows:

    <!DOCTYPE html>
<html>

<head>
    <title>Example</title>
</head>

<body>

<?php
    if (isset($_POST['Question']))
    {
        $menuVar = $_POST['fontFamily'];
    }
?>

<p id="info-req">How did you find about this site?</p>

<form name="TestMenu" method="post" id="marketing">
    <select name="Question">
        <option <?php if($menuVar=="----------") echo 'selected="selected"'; ?> value="----------">----------</option>
        <option <?php if($menuVar=="WebSearch") echo 'selected="selected"'; ?> value="WebSearch">Web Search</option>
        <option <?php if($menuVar=="SocialMedia") echo 'selected="selected"'; ?> value="SocialMedia">Social Media</option>
        <option <?php if($menuVar=="Wordofmouth") echo 'selected="selected"'; ?> value="Wordofmouth">Word of mouth</option>
        <option <?php if($menuVar=="Other") echo 'selected="selected"'; ?> value="Other">Other</option>
    </select>
  <input type="submit" />
</form>

</body>
</html>
È stato utile?

Soluzione

First of all, there is no $POST['fontFamily'] variable in your form. Why are you trying to use it? You should use $_POST['Question'] in order to get this value.

So it should be:

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

Also you should init $menuVar if there's no $POST in order not to get a Notice: Undefined variable $menuVar. So in the end you code should be:

   if (isset($_POST['Question']))
    {
        $menuVar = $_POST['Question'];
    } else {
        $menuVar = "----------";
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top