swipeales swipeales - 6 months ago 13
PHP Question

using post method but undefined index

On first load page i get this undefined index error, i know why.. because post method never happened. So anyone can help me how to fix this simple?

<form id="checkserver" method="POST">

<select class="btn2" name="server" onchange="$('#checkserver').submit();">
<option value="all">Show all </option>
<option value="Russia" <?=($_POST["server"] == 'Russia')?'selected':''?>>Russia</option>
<option value="Europe" <?=($_POST["server"] == 'Europe')?'selected':''?>>Europe</option>
<option value="Indonesia" <?=($_POST["server"] == 'Indonesia')?'selected':''?>>Indonesia</option>

</select>
</form>

Answer

here your work around code is.

<form id="checkserver" method="POST">

<select class="btn2" name="server" onchange="$('#checkserver').submit();">
    <option value="all">Show all </option>
    <option value="Russia" <?=(isset($_POST["server"]) && $_POST["server"] == 'Russia')?'selected':''?>>Russia</option>
    <option value="Europe" <?=(isset($_POST["server"]) && $_POST["server"] == 'Europe')?'selected':''?>>Europe</option>
    <option value="Indonesia" <?=(isset($_POST["server"]) && $_POST["server"] == 'Indonesia')?'selected':''?>>Indonesia</option>

    </select>
</form>

you have to check first for $_POST["server"] is set or not with 2 condition if isset and then check value. i hope it will help you.