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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.