Jump to content

Dynamic drop down field works but when chosen, nothing happens.


snoreboy01

Recommended Posts

Hi there all and first thing, thank you for any help you can give on this. :)

I am making a game with php and I am still new to php. I have made a View Fighter page so you can view different fighters. This is done by a dynamic drop down box that gets the list of fighters names from the database and creates the drop down box.

Then you can choose which fighter to view, it stays ont he same page but the data is up dated with the data from the fighter you have chosen.

 

The drop down has the list of fighters, but when i choose, nothing happens.

 

---- code ----

 

viewFighter.php

 

 

<?php
session_start();
require "db_connect.php";

/* To get form data and set what fighter name to get the data for. */
$FName = $_POST['fighter'];

/* Qry 1 is getting the fighter details based on unique name. */
$qry1 = mysql_query( "SELECT * FROM fighters where fighterName='$FName'");
$num_rows_1 = mysql_num_rows( $qry1 );

/* Qry 2 is unique names for options values. */
$qry2 = "SELECT fighterName FROM fighters order by fighterName asc";
$result2 =mysql_query($qry2);
$num_rows_2 = mysql_num_rows( $result2 );


$NickName = $a_row["fighterName"];
$RealName = $a_row["fullName"];
$gender = $a_row["gender"];
$Tech= $a_row["tech"];
$Type=$a_row["charType"];
$Height= $a_row["height"];
$Weight=$a_row["weight"];
$Age = $a_row["age"];
$Rank = $a_row["rank"];
$Concept = $a_row["concept"];
$Signature = $a_row["signature"];

$STR = $a_row["Fstr"];
$DEX =$a_row["Fdex"];
$STA = $a_row["Fsta"];
$CHA = $a_row["Fcha"];
$MAN = $a_row["Fman"];
$APP = $a_row["Fapp"];
$PER = $a_row["Fper"];
$INT = $a_row["Fint"];
$WIT = $a_row["Fwit"];
$imageLocation = "/Fighters/chars/";
$imageName = "{$NickName}.jpg";
$imageFinal = "{$imageLocation}{$imageName}";

?>

<html>
<head>
<title>Fighter Details</title>
</head>
<body>
<br>
<a href="/Fighters/index.php">Home!</a>
<a href="createFighter.php">Create Fighter!</a>
<br><br>

<h4><?php echo $NickName ?></h4>
<br><br>

<form method="post" action="viewFighter.php">
Choose a Fighter!
<select name="fighter">
<?php while($rows_2=mysql_fetch_array( $result2 )) 
{ 
?>
   <option value="<?php echo $rows_2['fighterName']?>"><?php echo $rows_2['fighterName']?></option>
<? 
} 
?>
<input type="submit" value="Choose!">
</select>
</form>
<br>

<table border="1">
<tr>
    <table border="1">
<tr>
<td>Nick Name:</td>
<td width=20%><?php echo $NickName?></td>
<td>Technique:</td>
<td><?php echo $Tech?></td>
<td rowspan="7" colspan="3" width=25%><img src="<?php echo $imageFinal; ?>" height="200"/></td>
</tr>
<tr>
<td>Real Name:</td>
<td><?php echo $RealName?></td>
<td idth=20%>Gender:</td>
<td><?php echo $gender ?></td>
</tr>

<tr>
<td>Height:</td>
<td><?php echo $Height?> ft</td>
<td>Weight:</td>
<td><?php echo$Weight?> lbs</td>
</tr>
<tr>
<td>Type:</td>
<td><?php echo $Type?></td>
<td>Rank:</td>
<td><?php echo $Rank ?></td>
</tr>
<tr>
<td>Age:</td>
<td><?php echo $Age ?></td>
</tr>
<tr>
<td>Concept:</td>
<td colspan = "3"><?php echo $Concept?></td>
</tr>
<tr>
<td>Signature:</td>
<td colspan = "3"><?php echo $Signature?></td>
</tr>

<tr>
<td>Strength:</td>
<td><?php echo $STR ?></td>
<td>Charisma:</td>
<td><?php echo$CHA?></td>
<td>Perceptiom:</td>
<td><?php echo$PER?></td>
</tr>
<tr>
<td>Dexterity:</td>
<td><?php echo$DEX?></td>
<td>Manipulation:</td>
<td><?php echo$MAN?></td>
<td>Intelligence:</td>
<td><?php echo$INT?></td>
</tr>
<tr>
<td>Stamina:</td>
<td><?php echo$STA?></td>
<td>Appearance:</td>
<td><?php echo$APP?></td>
<td>Wits:</td>
<td><?php echo $WIT?></td>
</tr>
    </table>
</tr>
<tr>
<table border="1">
<tr>
<td>Alertness:</td>
<td width=20%><?php echo $Alert?></td>
<td>Blind Fighting:</td>
<td><?php echo $Blind?></td>
       <td>Academics:</td>
<td><?php echo $Academics?></td>
</tr>
</tr>
</table>
<br>
<br>

</body>
</html>

 

---- code ----

 

The page source when viewed shows me:

 

---- page source ----

 

<html> 
<head> 
<title>Fighter Details</title> 
</head> 
<body> 
<br> 
<a href="/Fighters/index.php">Home!</a> 
<a href="createFighter.php">Create Fighter!</a> 
<br><br> 

<h4></h4> 
<br><br> 

<form method="post" action="viewFighter.php"> 
Choose a Fighter!
<select name='fighter'> 
   <option value="Blake">Blake</option> 
   <option value="Bone Breaker">Bone Breaker</option> 
   <option value="Destroyer">Destroyer</option> 
   <option value="Dominator">Dominator</option> 
   <option value="Hank">Hank</option> 
<input type="submit" value="Choose!"> 
</select> 
</form> 
<br> 

<table border="1"> 
<tr> 
    <table border="1"> 
<tr> 
<td>Nick Name:</td> 
<td width=20%></td> 
<td>Technique:</td> 
<td></td> 
<td rowspan="7" colspan="3" width=25%><img src="/Fighters/chars/.jpg" height="200"/></td> 
</tr> 
<tr> 
<td>Real Name:</td> 
<td></td> 
<td idth=20%>Gender:</td> 
<td></td> 
</tr> 

<tr> 
<td>Height:</td> 
<td> ft</td> 
<td>Weight:</td> 
<td> lbs</td> 
</tr> 
<tr> 
<td>Type:</td> 
<td></td> 
<td>Rank:</td> 
<td></td> 
</tr> 
<tr> 
<td>Age:</td> 
<td></td> 
</tr> 
<tr> 
<td>Concept:</td> 
<td colspan = "3"></td> 
</tr> 
<tr> 
<td>Signature:</td> 
<td colspan = "3"></td> 
</tr> 

<tr> 
<td>Strength:</td> 
<td></td> 
<td>Charisma:</td> 
<td></td> 
<td>Perceptiom:</td> 
<td></td> 
</tr> 
<tr> 
<td>Dexterity:</td> 
<td></td> 
<td>Manipulation:</td> 
<td></td> 
<td>Intelligence:</td> 
<td></td> 
</tr> 
<tr> 
<td>Stamina:</td> 
<td></td> 
<td>Appearance:</td> 
<td></td> 
<td>Wits:</td> 
<td></td> 
</tr> 
    </table> 
</tr> 
<tr> 
<table border="1"> 
<tr> 
<td>Alertness:</td> 
<td width=20%></td> 
<td>Blind Fighting:</td> 
<td></td> 
       <td>Academics:</td> 
<td></td> 
</tr> 
</tr> 
</table> 
<br> 
<br> 



</body> 
</html> 

 

---- page source ----

 

Again, thank you for any help you can give.

:)

 

 

Link to comment
Share on other sites

hey snoreboy, first thing first, when admins come by, they'll tell you to put all your code between either

code here

or

code here

eaiser to read... and maybe put your code in each page in separate

...

or

...

so people know how many pages you're talking about...

come by later,,,,,

;)

Link to comment
Share on other sites

Haave you tried clicking the submit button? How else are you going to get data to the script?

 

Anyway, if you don't want it to reload the page, you have to add the onchange attribute to the <select> and use AJAX to POST the value to your PHP script and then replace whatever part of the page it is you want to be dynamic.

 

I think what IrOnMaSk was trying to say was, please put your code inside


or


tags.

Link to comment
Share on other sites

You say

Then you can choose which fighter to view, it stays ont he same page but the data is up dated with the data from the fighter you have chosen.

 

But, that is not totally clear. You currently have a submit button so that tells me youwould want to submit the form and refresh the page. When you state "stays on the same page" that could imply using AJAX to update the page without needing to submit/refresh the page. But you can, and should, do both. So, first get the page working by submitting the page normally, then once you get that working youcan work on adding AJAX handling if you want.

 

So, looking at your current code, some things to check:

 

The action attribute in the FORM tag is "viewFighter.php" is that the same page of the script? If so, you can jsut remove the value in the action attribute so if you ever change the name of the file it will still work. But, if it is a different page, then it obviously won't work.

 

OK, I think this is your problem. You have several lines where you are defining variables, such as

$NickName = $a_row["fighterName"];
$RealName = $a_row["fullName"];
$gender = $a_row["gender"];
...

 

However, I don't see where $a_row is ever defined! So, nothing would be displayed in the page for those values. You would have found this if you had turned on full error reporting.

 

EDIT: I also see that your SUBMIT button is INSIDE  your selection list immediately following the options, but before the closing "</select>" tag

Link to comment
Share on other sites

OK, I've gone through your code and there were a lot of problems. I rewrote the logic and added some rudimentary error handling. I wasn't able to test this since I don't have your database, so there may be some syntax errors. NOTE: I turned on full error reporting on the first line. If any errors such as db results missing were to occur, you would get a lot of PHP warning messages because the variables for the table data would not be defined and the code would try to echo them. Ideally, you should be conditionally creating that table only if results were obtained to fill it.

 

<?php
//Turn all full error reporting
error_reporting(E_ALL);

session_start();
require "db_connect.php";

//Define error message var for error handling
$errorMsg = false;

//Defined the selected fighter is sent in POST data
$selectedFighter = isset($_POST['fighter']) ? trim($_POST['fighter'])  : false;

//Query to get list of fighters for select list
$query = "SELECT fighterName FROM fighters ORDER BY fighterName ASC";
$result = mysql_query($query);
if(!$result)
{
    $errorMsg = "Unable to get list of fighters.";
}
elseif(mysql_num_rows($result)<1)
{
    $errorMsg = "There are no fighters to select from.";
}
else
{
    //Create select list options
    $fighterOptions = '';
    while($row = mysql_fetch_assoc($result))
    {
        $selected = ({$row['fighterName'] == $selectedFighter) ? ' selected="selected"' : '';
        $fighterOptions .= "<option value=\"{$row['fighterName']}\"{$selected}>{{$row['fighterName']}}</option>\n"
    }
}

//Query to get details of selected fighter if set
if($selectedFighter!==false && $errorMsg!==false)
{
    $selectedFighter = mysql_real_escape_string($selectedFighter);
    $query = "SELECT * FROM fighters WHERE fighterName='{$selectedFighter}'"
    $result = mysql_query($query);

    if(!$result)
    {
        $errorMsg = "Error retrieving information for selected fighter.";
    }
    elseif(mysql_num_rows($result)<1)
    {
        $errorMsg = "The selected fighter does not exist.";
    }
    else
    {
        //Get data
        $fighter = mysql_fetch_assoc($result);

        $NickName  = htmlentities($fighter["fighterName"]);
        $RealName  = htmlentities($fighter["fullName"]);
        $gender    = htmlentities($fighter["gender"]);
        $Tech      = htmlentities($fighter["tech"]);
        $Type      = htmlentities($fighter["charType"]);
        $Height    = htmlentities($fighter["height"]);
        $Weight    = htmlentities($fighter["weight"]);
        $Age       = htmlentities($fighter["age"]);
        $Rank      = htmlentities($fighter["rank"]);
        $Concept   = htmlentities($fighter["concept"]);
        $Signature = htmlentities($fighter["signature"]);

        $STR = htmlentities($fighter["Fstr"]);
        $DEX = htmlentities($fighter["Fdex"]);
        $STA = htmlentities($fighter["Fsta"]);
        $CHA = htmlentities($fighter["Fcha"]);
        $MAN = htmlentities($fighter["Fman"]);
        $APP = htmlentities($fighter["Fapp"]);
        $PER = htmlentities($fighter["Fper"]);
        $INT = htmlentities($fighter["Fint"]);
        $WIT = htmlentities($fighter["Fwit"]);
        $imageLocation = "/Fighters/chars/";
        $imageName = "{$NickName}.jpg";
        $imageSrc = "{$imageLocation}{$imageName}";
    }
}

//Format the error message
if($errorMsg!==false)
{
    $errorMsg = "<span style=\"color:#ff0000;\">{$errorMsg}<br><br>\n";
}
else
{
    $errorMsg = '';
}

?>
<html>
  <head>
    <title>Fighter Details</title>
  </head>
<body>
<br>
<a href="/Fighters/index.php">Home!</a>
<a href="createFighter.php">Create Fighter!</a>
<br><br>
<?php echo $errorMsg; ?>

<form method="post" action="viewFighter.php">
  Choose a Fighter!
  <select name="fighter">
    <?php echo $fighterOptions; ?>
  </select>
  <input type="submit" value="Choose!">
</form>
<br>

<h4><?php echo $NickName; ?></h4>
<br><br>

<table border="1">
  <tr>
    <td>Nick Name:</td>
    <td width="20%"><?php echo $NickName; ?></td>
    <td>Technique:</td>
    <td><?php echo $Tech; ?></td>
    <td rowspan="7" colspan="2" width="25%"><img src="<?php echo $imageSrc; ?>" height="200"/></td>
  </tr>
  <tr>
    <td>Real Name:</td>
    <td><?php echo $RealName?></td>
    <td width="20%">Gender:</td>
    <td><?php echo $gender ?></td>
  </tr>
  <tr>
    <td>Height:</td>
    <td><?php echo $Height; ?> ft</td>
    <td>Weight:</td>
    <td><?php echo $Weight; ?> lbs</td>
  </tr>
  <tr>
    <td>Type:</td>
    <td><?php echo $Type; ?></td>
    <td>Rank:</td>
    <td><?php echo $Ran;k ?></td>
  </tr>
  <tr>
    <td>Age:</td>
    <td colspan="3"><?php echo $Age; ?></td>
  </tr>
  <tr>
    <td>Concept:</td>
    <td colspan="3"><?php echo $Concept; ?></td>
  </tr>
  <tr>
    <td>Signature:</td>
    <td colspan="3"><?php echo $Signature; ?></td>
  </tr>
  <tr>
    <td>Strength:</td>
    <td><?php echo $STR; ?></td>
    <td>Charisma:</td>
    <td><?php echo $CHA; ?></td>
    <td>Perceptiom:</td>
    <td><?php echo $PER; ?></td>
  </tr>
  <tr>
    <td>Dexterity:</td>
    <td><?php echo $DEX; ?></td>
    <td>Manipulation:</td>
    <td><?php echo $MAN; ?></td>
    <td>Intelligence:</td>
    <td><?php echo $INT; ?></td>
  </tr>
  <tr>
    <td>Stamina:</td>
    <td><?php echo $STA; ?></td>
    <td>Appearance:</td>
    <td><?php echo $APP; ?></td>
    <td>Wits:</td>
    <td><?php echo $WIT; ?></td>
  </tr>
  <tr>
    <td>Alertness:</td>
    <td width="20%"><?php echo $Alert; ?></td>
    <td>Blind Fighting:</td>
    <td><?php echo $Blind; ?></td>
    <td>Academics:</td>
    <td><?php echo $Academics; ?></td>
  </tr>
</table>

</body>
</html>

Link to comment
Share on other sites

 

Thank you for all your replies.

When I click my button to submit, I want it to refresh the current page with the new data. (The form calls the same page.)

 

The problem I had was when I made changes to the code, I didn't change the $a_row to the new name of $num_rows_1 also during my changes, I used mysql_num_rows again instead of mysql_fetch_array.

 

Changed those 2 things and all works fine now. :)

 

I didn't know about error_reporting(E_ALL), I thought the browser reported all errors when the page was rendered. I shall include that in from now on, thank you. :)

 

The <input type="submit" value="Choose!"> being after the <options> and before the </select> tag works fine.

 

Thank you all.

 

 

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.