PHP > can't get my variable through from page1 to page2

  • danfuniel
  • Newbie
  • Newbie
  • danfuniel
  • Posts: 6

Post 3+ Months Ago

Hey,
I don't understand why I'm getting the variable in my p2.php when I set it in p1.php the following way:

in p1.php I have::
----------------------------
<code><form action="p2.php" method="post">
<select name="name" size="1" >

PHP Code: [ Select ]
 
while ($myrow = mysql_fetch_array($result)) {
 
    printf("<option value=%s>%s", $PHP_SELF, $myrow['name'],"</option>");
 
  }
 
  ?>
 
</select>
 
<br>
 
<input type="submit" value="SHOW by NAME">
 
</form>
 
 
  1.  
  2. while ($myrow = mysql_fetch_array($result)) {
  3.  
  4.     printf("<option value=%s>%s", $PHP_SELF, $myrow['name'],"</option>");
  5.  
  6.   }
  7.  
  8.   ?>
  9.  
  10. </select>
  11.  
  12. <br>
  13.  
  14. <input type="submit" value="SHOW by NAME">
  15.  
  16. </form>
  17.  
  18.  

--------------------------
and in my p2.php I try to just display the variable "name"
PHP Code: [ Select ]
 
$first = addslashes($_POST['name']);
 
 
 
printf("SELECT name FROM doctors where name='".$first."'\";");
 
 
  1.  
  2. $first = addslashes($_POST['name']);
  3.  
  4.  
  5.  
  6. printf("SELECT name FROM doctors where name='".$first."'\";");
  7.  
  8.  


I also tried printing it directly like
printf(.$_POST['name'].);
and nothing ...

:arrow: PLEASE helppppp
Do I need to change anything for this to work in my php.ini file in the root folder??

Thanx for any suggestions :!:
8)
  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

It seems like you are setting the value of the name option to $PHP_SELF.

I don't use printf very often, but shouldn't the line be like this?

Quote:
printf("<option value=%s>%s", $myrow['name'], $myrow['name'],"</option>");
  • danfuniel
  • Newbie
  • Newbie
  • danfuniel
  • Posts: 6

Post 3+ Months Ago

Thanx Cafu,
that $PHP_SELF shouldn't have been there.
But now I get just

SELECT name FROM doctors where name='Dr.'";

on p2.php

code for that is
PHP Code: [ Select ]
 
printf("SELECT name FROM doctors where name='".$_POST['name']."';");
 
 
  1.  
  2. printf("SELECT name FROM doctors where name='".$_POST['name']."';");
  3.  
  4.  

whereas I should be getting the full name, such as Dr. Valery Green ...
Does anybody know why am I getting just Dr. without the name?
Thank you
8)
  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

printf is for formatting text. Try that same line with "print" or "echo" and see if that's your problem.
  • danfuniel
  • Newbie
  • Newbie
  • danfuniel
  • Posts: 6

Post 3+ Months Ago

I tried echo and print on that print statement, but still getting just Dr. and not the whole nameeeeeeeeee

:(
  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

show me your new option statement, the one where you removed the PHP_SELF
  • danfuniel
  • Newbie
  • Newbie
  • danfuniel
  • Posts: 6

Post 3+ Months Ago

i'm using yours ...

PHP Code: [ Select ]
 
printf("<option value=%s>%s", $myrow['name'], $myrow['name'],"</option>");
 
 
  1.  
  2. printf("<option value=%s>%s", $myrow['name'], $myrow['name'],"</option>");
  3.  
  4.  
  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

Can you view the document source to make sure that line is working correctly?

Again, I don't use printf much so, for the sake of debugging, I would probably change that line to:

Code: [ Select ]
print("<option value='" . $myrow['name'] . "'>" . $myrow['name'] . "</option>");


Also, in typing that I noticed you aren't putting quotes around your option value. I don't know if that's the problem, but it couldn't hurt putting them in there, especially for values that have spaces in them like Dr. Valery Green.
  • danfuniel
  • Newbie
  • Newbie
  • danfuniel
  • Posts: 6

Post 3+ Months Ago

YOU'RE A GENIUS. it workssssssssss :)
THANK YOU!!!!!!!!
  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

Great :)


You may want to consider altering your code to use numerical IDs. I am assuming that in your doctor table you have some sort of id field, perhaps an auto-incrementing one. If so, you could change your query that builds the select box to:
Code: [ Select ]
Select id, name from doctors

then change your code in the page to be:

Code: [ Select ]
print("<option value='" . $myrow['id'] . "'>" . $myrow['name'] . "</option>");



This isn't a necessary change now, but what if someday your application has advanced and you have 3000 doctors in your database.

Code: [ Select ]
"select name from doctors where id = ".$_POST['id']

is a lot faster than
Code: [ Select ]
"select name from doctors where name = ".$_POST['name']

because you are doing a numerical comparison rather than a text comparision.


Now if you have another table called "insurance_claims" that is referenced by the doctor you use the ID value in this table rather than the doctor name. If these 4000 doctors are getting 10ish claims a day this table could get kind of big and selecting all claims for one doctor will be considerably faster if you are selecting where doctor_id = 42 rather than where doctor = "Dr. Valery Green"

[/code]
  • danfuniel
  • Newbie
  • Newbie
  • danfuniel
  • Posts: 6

Post 3+ Months Ago

Yeah, good thinking Cabu :)
way to gooo 8)

Thanks so much for your help!

:arrow: You deserve 500 pts. :shock:

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: Liamw411 and 112 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.