matthewst Posted January 21, 2008 Share Posted January 21, 2008 heres my code <?php include('include/db_con.php'); $slacker_current_runners = mysql_query("SELECT run_user,run_distance,run_time,run_calories,run_date FROM run", $db_link); while($row_current_runners = mysql_fetch_array($slacker_current_runners)) { $run_user = $row_current_runners['run_user']; $run_distance = $row_current_runners['run_distance']; $run_time = $row_current_runners['run_time']; $run_calories = $row_current_runners['run_calories']; $run_date = $row_current_runners['run_date'];} ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><style type="text/css"> <!-- body,td,th { color: #FFFFFF; } body { background-color: #000000; } --> </style> </head> <body> <form name="run" action="<?php $_SERVER[’PHP_SELF’]; ?>" method="post"> Name: <select name="runner_name" class="formTextbox" size="1"> <option value="">-- Select a Runner --</option> <?php $runners = mysql_query("SELECT run_user FROM run_users", $db_link); while($row_runners = mysql_fetch_array($runners)) { $run_user_selected = $row_runners['run_user']; ?> <option value="<?=$run_user_selected;?>"><? echo "$run_user_selected";?></option> <?php } ?> </select> <input type="submit" name="submit" class="formTextbox" value="Submit"> </form> <?php if($submit) { echo "$run_user"; echo "$run_distance"; echo "$run_time"; echo "$run_calories"; echo "$run_date"; echo "got it";} ?> </body> </html> the $run_user_selected shows up in the drip down box but when i hit submit the if ($submit) does not pop in like it should Quote Link to comment Share on other sites More sharing options...
taith Posted January 21, 2008 Share Posted January 21, 2008 you need to echo out the variable... instead of just stating it... <form name="run" action="<?=$_SERVER[’PHP_SELF’]; ?>" method="post"> or <form name="run" action="<?php echo $_SERVER[’PHP_SELF’]; ?>" method="post"> Quote Link to comment Share on other sites More sharing options...
marcus Posted January 21, 2008 Share Posted January 21, 2008 try echoing it <?php echo $_SERVER['PHP_SELF']; ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted January 21, 2008 Share Posted January 21, 2008 Just remove the entire action line. Also, re think your logic on that page. Quote Link to comment Share on other sites More sharing options...
matthewst Posted January 21, 2008 Author Share Posted January 21, 2008 when i change the method from post to get i get this in the address bar "www.website.com/index.php?runner_name=&submit=Submit" I tried echoing the php_self still no luck Quote Link to comment Share on other sites More sharing options...
matthewst Posted January 22, 2008 Author Share Posted January 22, 2008 here's how it should work (just the php portion) user opens the page selects a name from the drop down menu clicks submit the page then displays the records pertaining to the chosen individual I can't get the page to submit the $runner_name to itself Quote Link to comment Share on other sites More sharing options...
tapos Posted January 22, 2008 Share Posted January 22, 2008 <form name="run" action="<?php $_SERVER[’PHP_SELF’]; ?>" method="post"> this should be <form name="run" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Please use the single quote perfectly. -- Thanks, http://tapos.wordpress.com Quote Link to comment Share on other sites More sharing options...
trq Posted January 22, 2008 Share Posted January 22, 2008 <form name="run" action="<?php $_SERVER[PHP_SELF]; ?>" method="post"> this should be <form name="run" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Please use the single quote perfectly. -- Thanks, http://tapos.wordpress.com Or better still.... <form name="run" method="post"> Quote Link to comment Share on other sites More sharing options...
matthewst Posted January 22, 2008 Author Share Posted January 22, 2008 Here's the new code: <?php include('include/db_con.php'); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><style type="text/css"> <!-- body,td,th { color: #FFFFFF; } body { background-color: #000000; } --> </style> </head> <body> <form name="run" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> ///////////////////also tried it without the action statement Name: <select name="runner_name" class="formTextbox" size="1"> <option value="">-- Select a Runner --</option> <?php $runners = mysql_query("SELECT run_user FROM run_users", $db_link); while($row_runners = mysql_fetch_array($runners)) { $run_user_selected = $row_runners['run_user']; ?> <option value="<?php $run_user_selected;?>"><?php echo "$run_user_selected";?></option> <?php } ?> </select> <input type="submit" name="submit" class="formTextbox" value="Submit"> </form> <?php if($submit) { $slacker_current_runners = mysql_query("SELECT run_user,run_distance,run_time,run_calories,run_date FROM run WHERE run_user = '$run_user_selected'", $db_link); ////////////////////////////also tried /////$slacker_current_runners = mysql_query("SELECT run_user,run_distance,run_time,run_calories,run_date FROM run WHERE run_user = '$runner_name'", $db_link); while($row_current_runners = mysql_fetch_array($slacker_current_runners)) { $run_user = $row_current_runners['run_user']; $run_distance = $row_current_runners['run_distance']; $run_time = $row_current_runners['run_time']; $run_calories = $row_current_runners['run_calories']; $run_date = $row_current_runners['run_date'];} echo "$run_user"; echo "$run_distance"; echo "$run_time"; echo "$run_calories"; echo "$run_date"; echo "got it";} ?> </body> </html> Obviously, I'm not as good with php as you guys. What am i missing? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 22, 2008 Share Posted January 22, 2008 Let's break some of this down: <?php include('include/db_con.php'); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><style type="text/css"> <!-- body,td,th { color: #FFFFFF; } body { background-color: #000000; } --> </style> </head> <body> Looking good so far... <form name="run" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> ///////////////////also tried it without the action statement The default value for 'action' is itself. So, if you want the page to submit to itself, just leave it out. This makes your page more portable. Also, for this particular use, I would stick with the GET method. This way the search term will be in the URL. There are a few reasons for this, but the big reason is it gives the user a URL they can bookmark,email to a friead, etc. Name: <select name="runner_name" class="formTextbox" size="1"> <option value="">-- Select a Runner --</option> <?php $runners = mysql_query("SELECT run_user FROM run_users", $db_link); while($row_runners = mysql_fetch_array($runners)) { $run_user_selected = $row_runners['run_user']; ?> You are using the wrong mysql function here. Use mysql_fetch_assoc() instead of mysql_fetch_array() here. The function arguments should remain unchanged. <option value="<?php $run_user_selected;?>"><?php echo "$run_user_selected";?></option> The first PHP statement is just stating a variable, and is basically meaningless. You need to echo it like the second PHP statement does. Also, in the second PHP statement, you don't need the quotes around the variable. Furthermore, unless you can GUARANTEE the run_users will have no 'html characters', you should wrap the variables with the htmlspecialchars() function. Just to clarify, the line should be: <option value="<?php echo htmlspecialchars($run_user_selected);?>"><?php echo htmlspecialchars($run_user_selected);?></option> <?php } ?> </select> <input type="submit" name="submit" class="formTextbox" value="Submit"> </form> Looks good. But, I would remove the name="submit" attribute as it won't be needed, and will produce a cleaner url query string. <?php if($submit) { If you were using POST, this should be $_POST['submit']. But, since we are change to GET, and the submit button will no longer be passed as data, it's easiest to just test for a value of runner_name. To do that, let's use: if(strlen($_GET['runner_name'])){ $slacker_current_runners = mysql_query("SELECT run_user,run_distance,run_time,run_calories,run_date FROM run WHERE run_user = '$run_user_selected'", $db_link); ////////////////////////////also tried /////$slacker_current_runners = mysql_query("SELECT run_user,run_distance,run_time,run_calories,run_date FROM run WHERE run_user = '$runner_name'", $db_link); To access the value of variable passed, you should use $_GET['runner_name']. Also, for security reasons, let's use the mysql_real_escape_string() function to prevent people from passing malicious code. So, the query should be: $slacker_current_runners = mysql_query("SELECT run_user,run_distance,run_time,run_calories,run_date FROM run WHERE run_user = '".mysql_real_escape_string($_GET['runner_name'])."'", $db_link); while($row_current_runners = mysql_fetch_array($slacker_current_runners)) { Again, use mysql_fetch_assoc() $run_user = $row_current_runners['run_user']; $run_distance = $row_current_runners['run_distance']; $run_time = $row_current_runners['run_time']; $run_calories = $row_current_runners['run_calories']; $run_date = $row_current_runners['run_date'];} echo "$run_user"; echo "$run_distance"; echo "$run_time"; echo "$run_calories"; echo "$run_date"; echo "got it";} ?> </body> </html> This part looks fine. But, unless you have a specific reason for storing the values into new variables, you can just echo the values from $row_current_runners directly, like: echo $row_current_runners['run_user']; Quote Link to comment Share on other sites More sharing options...
nikefido Posted January 22, 2008 Share Posted January 22, 2008 im confused on the mysql_fetch_assoc() - it seems to act the same way as mysql_fetch_array()? - Either way you reference the array element associatively, no? Quote Link to comment Share on other sites More sharing options...
revraz Posted January 22, 2008 Share Posted January 22, 2008 mysql_fetch_array will get both, a numeric and associate. mysql_fetch_assoc will only get associate im confused on the mysql_fetch_assoc() - it seems to act the same way as mysql_fetch_array()? - Either way you reference the array element associatively, no? Quote Link to comment Share on other sites More sharing options...
resago Posted January 22, 2008 Share Posted January 22, 2008 unless you have globals turned on, you're not doing anything with the form. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 22, 2008 Share Posted January 22, 2008 yeah...sorry about that...i was confusing mysql_fetch_array and mysql_fetch_row. looks like i've already used today's slip up allotment ;-) Quote Link to comment Share on other sites More sharing options...
matthewst Posted January 22, 2008 Author Share Posted January 22, 2008 ;D Thanks Everyone! Thanks rhodesa! You Guys ROCK!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.