B.J. A.A. B.J. A.A. - 4 months ago 11
PHP Question

Why PHP doesn't receive values from my select?


Notice: Undefined index: perm_tipo in C:\xampp\htdocs\admin_reg_usuario.php on line 6


I get this error from my PHP page where is supposed to receive a value from a
html select form, I send this info with POST, this is the code from my select:

<select name="perm_tipo">
<?php
$con=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("test",$con) or die(mysql_error());
$sql = "SELECT id_permiso,tipo_permiso FROM cat_permisos";
$rs = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($rs)){
echo "<option value='".$row["id_permiso"]."'>".$row["tipo_permiso"]."</option>";
}
mysql_free_result($rs);

mysql_close($con);
?>
</select>


and another php page to use the values I use this

$permiso = $_POST['perm_tipo'];


I'm not quite sure what I'm doing wrong, the syntax and name are fine.

EDIT: now is working,the var_dump($_POST); code made it work??, =S

Tek Tek
Answer

Okay so you're submitting the form to the same PHP page.

That means when you first visit the page, $_POST is empty. After you click submit $_POST is populated when the form is sent to the same page with perm_tipo.

What you need to make sure is that $_POST is sent to your PHP page with isset. This will detect when your form is submitted. This will also not execute the code when $_POST is empty which is why you get that "Notice:" error.

<?php

if (isset($_POST['perm_tipo']))
{
    $con=mysql_connect("localhost","root","") or die(mysql_error());

    mysql_select_db("test",$con) or die(mysql_error());              
    $sql = "SELECT id_permiso,tipo_permiso FROM cat_permisos";              
    $rs = mysql_query($sql) or die(mysql_error());

    while($row = mysql_fetch_array($rs)){ 
        echo "<option value='".$row["id_permiso"]."'>".$row["tipo_permiso"]."</option>";            
    }
    mysql_free_result($rs); 

    mysql_close($con);
}

?>
Comments