LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   newbie having trouble with posting my result from an array (https://www.linuxquestions.org/questions/linux-software-2/newbie-having-trouble-with-posting-my-result-from-an-array-4175461538/)

ashevillan 05-11-2013 08:29 AM

newbie having trouble with posting my result from an array
 
so i have a program and when the person selects an animal i need to display the animals name and coresponding age. so what do i need to put in my print statement to display that? i tried ".$_POST[animal]" but that didnt work

<!-- Author:
Date:
File: lifespans.php
Purpose:
-->

<html>
<head>
<title>Lifespans</title>
<link rel ="stylesheet" type="text/css" href="sample.css" />
</head>

<body>
<h1>Lifespans</h1>
<?php

$animal = $_POST['animal'];
$animal['Mouse'] = 4;
$animal['Queen Bee'] = 5;
$animal['Squirrel'] = 20;
$animal['Ratlesnake'] = 22;
$animal['Pheasant'] = 27;
$animal['Mallard Duck'] = 29;
$animal['Bear'] = 40;
$animal['Box Turtle'] = 123;

print ("<p>The life span of the </p>";



?>
<a href="lifespans.html"> Try another animal..</a>
</body>
</html>

kedarp 05-11-2013 10:03 AM

There is no logic in the posted code.
You have to clear your HTML and the <form> tag.

Then go for PHP code.

For example, your HTML code should look like this.
Code:

<body>
<form action="animals.php" method="post">
Select an animal:
<select name="animal">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="horse">Horse</option>
</select>
</form>
</body>
</html>


ashevillan 05-12-2013 01:01 AM

the html isnt the problem i have another page with the html and the selection menu, my problem is that when i run the code whatever variable is chosen the first letter gets changed to a 1. examle: 1heasant, 1ear, etc...

kedarp 05-12-2013 05:34 AM

Use a different variable to collect the animal name and for the age.

Code:

$animal = $_POST['animal'];

$age['Mouse'] = 4;
$age['Queen Bee'] = 5;
$age['Squirrel'] = 20;
$age['Ratlesnake'] = 22;
$age['Pheasant'] = 27;
$age['Mallard Duck'] = 29;
$age['Bear'] = 40;
$age['Box Turtle'] = 123;

And then print.
Code:

echo "The life span of " . $animal . " is " . $age[$animal];


All times are GMT -5. The time now is 09:33 AM.