Jump to content

[SOLVED] can't get $_SERVER[’PHP_SELF’] to work with if ($submit)


matthewst

Recommended Posts

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

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">

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

<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

<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">

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?

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'];

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.