Page 1 of 2

Problem passing variable from a select

Posted: Wed Jul 07, 2010 1:47 pm
by jotae
The code:

Code: Select all

....more code
?>
  <h4>Seleccione la receta y pulse OK!</h4>
  <form name="receta" action="recetas.php" method="get">
  <select class="temas" name="sureceta" size=5>
   <?php
    while($row=mysql_fetch_assoc($result)){ 
    echo '<option value='.$row["nombre"].'>'.$row["nombre"].'</option>';
   }

?>
   </select>
   <br>
   <input type="submit" name="recetas" value="OK!">
  </form>

....more code
-------------------------------
//recetas.php

.....more code
<?php
    $receta=$_POST['$sureceta'];
    echo "Esta es la receta" .$receta ."<br>";
    echo var_dump($sureceta); //Result=NULL.
    echo var_dump($receta); // Result=NULL
?>
The Select working perfect in index.php but in recetas.php the variable is NULL. Can help me, please? Thx.

Re: Problem passing variable from a select

Posted: Wed Jul 07, 2010 1:51 pm
by Jade
Are you posting $sureceta to the recetas.php page? You have to pass it to every page you want to use it, so either you need to make it a $_SESSION variable or you'll need to post it as a hidden field in the form.

Re: Problem passing variable from a select

Posted: Wed Jul 07, 2010 1:58 pm
by AbraCadaver
Your form says method="get", but you're trying to access $_POST['$sureceta'].

Also, read up on the difference between single and double quotes: http://us3.php.net/manual/en/language.types.string.php

Re: Problem passing variable from a select

Posted: Wed Jul 07, 2010 2:41 pm
by Jade
Hahah I completely missed that. Looks like my 8 hours are up for the day!

Re: Problem passing variable from a select

Posted: Wed Jul 07, 2010 5:32 pm
by jotae
No, sorry. is an error in my post. Really in my real code is method="get" and in the recetas.php $_GET['$sureceta']. The real code is:

Code: Select all

?>
//Index.php
    <h4>Seleccione la receta y pulse OK!</h4>
    <form name="receta" action="recetas.php" method="get">
     <select class="temas" name="sureceta" size=5>
      <?php
       while($row=mysql_fetch_assoc($result)){ 
        echo '<option value='.$row["nombre"].'>'.$row["nombre"].'</option>' ;
       }
      ?>
     </select>
     <br>
     <input type="submit" name="OK" value="OK!">
    </form>
//recetas.php

<?php
//This code is only for testing the variable.    
    $receta=$_GET["$sureceta"];
    echo "esta es la receta" . $receta ."<br>";
    echo var_dump($receta); //Result=NULL
   ?>
But it doesn't work this way $_GET["$sureceta"]; neither this way $_GET['$sureceta'];

Re: Problem passing variable from a select

Posted: Wed Jul 07, 2010 8:03 pm
by jotae
In this way work:

Code: Select all

<?php
    $receta= $_GET['sureceta'];
    echo "esta es la receta " .$receta."<br>";
?>
without $ (dollar sign) but continue the problem. For example: if the name of Recipe is "BEANS WITH PORK" only read the first word = BEANS. I appreciate any help

Re: Problem passing variable from a select

Posted: Thu Jul 08, 2010 9:50 am
by Jade
Make sure you have quotes around the values:

Code: Select all

<option value="Beans with Pork">Beans with Pork</option>
Instead of:

Code: Select all

<option value=Beans with Pork>Beaks with Pork</option>
In this second example it will drop off the "with Pork" thinking that those are just invalid characters/markup at the end of the tag.

Re: Problem passing variable from a select

Posted: Thu Jul 08, 2010 9:02 pm
by jotae
Please see what happen: this is a little demo of the project for test the problem: (The code is the same in this post)
Actually I try this two lines:

Code: Select all

echo '<option value='.$row["nombre"].'>'.$row["nombre"].'</option>' ; //double
echo '<option value='.$row['nombre'].'>'.$row['nombre'].'</option>' ;    //single
httP://www.prolatin.net/colmesa/indexpr.php

Now, see what happen if you see the source:

form name="receta" action="indexpr.php" method="get">
<select class="temas" name="sureceta" size=5>
<option value=AJI CASERO>AJI CASERO</option>
<br>
<input type="submit" name="OK" value="OK!" >
</form>

<br>esta es la receta AJI<br>

The AJI is active and CASERO not. Another thing: see in the demo that SELECT read records correctly, the complete words. So, no problem with DB but pass only the first word. I'm crazy with this problem :-) Thanks for you time..

Re: Problem passing variable from a select

Posted: Thu Jul 08, 2010 9:32 pm
by agriz

Code: Select all

echo '<option value="'.$row["nombre"].'">'.$row["nombre"].'</option>';
Try something to echo the options and be sure to get it like this.

Code: Select all

$receta= $_GET['sureceta'];
It is not correct

Code: Select all

$_GET["$sureceta"];
//Wrong Method to get the select value

Re: Problem passing variable from a select

Posted: Thu Jul 08, 2010 10:36 pm
by jotae
Thx. Make all the changes suggested but result= NULL.

Re: Problem passing variable from a select

Posted: Thu Jul 08, 2010 11:09 pm
by agriz
Your option value is assigned like this in your site

Code: Select all

<option aguacate="" de="" value="AJI">AJI DE AGUACATE</option>
You have to fix this. If you are having problem with PHP quotes, then try like this

Code: Select all

<?php
       while($row=mysql_fetch_assoc($result)){
?>
        <option value="<?=$row['nombre']?>"><?=$row['nombre']?></option>
<?php
       }
?>

Re: Problem passing variable from a select

Posted: Thu Jul 08, 2010 11:55 pm
by jotae
Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Two days working with this and now you solved!!!!

See working the demo:

httP://www.prolatin.net/colmesa/indexpr.php.

Really, agriz, I am very grateful with you. Now I will be able to finish developing the project. Best regards.

Re: Problem passing variable from a select

Posted: Thu Jul 08, 2010 11:59 pm
by agriz
Great :-)

All the best

Re: Problem passing variable from a select

Posted: Fri Jul 09, 2010 2:51 pm
by jotae
Solved

Re: Problem passing variable from a select

Posted: Fri Jul 09, 2010 7:40 pm
by agriz

Code: Select all

$codigo = mysql_query("SELECT ID FROM receta WHERE nombre='".$receta."'");
$result = mysql_fetch_object($codigo);

echo $result->ID;
Now try this