Problem passing variable from a select

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Problem passing variable from a select

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Problem passing variable from a select

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Problem passing variable from a select

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Problem passing variable from a select

Post by Jade »

Hahah I completely missed that. Looks like my 8 hours are up for the day!
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Problem passing variable from a select

Post 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'];
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Problem passing variable from a select

Post 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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Problem passing variable from a select

Post 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.
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Problem passing variable from a select

Post 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..
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: Problem passing variable from a select

Post 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
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Problem passing variable from a select

Post by jotae »

Thx. Make all the changes suggested but result= NULL.
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: Problem passing variable from a select

Post 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
       }
?>
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Problem passing variable from a select

Post 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.
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: Problem passing variable from a select

Post by agriz »

Great :-)

All the best
jotae
Forum Commoner
Posts: 25
Joined: Fri Jul 02, 2010 5:49 pm

Re: Problem passing variable from a select

Post by jotae »

Solved
Last edited by jotae on Fri Jul 09, 2010 9:00 pm, edited 1 time in total.
agriz
Forum Contributor
Posts: 106
Joined: Sun Nov 23, 2008 9:29 pm

Re: Problem passing variable from a select

Post 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
Post Reply