LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-11-2013, 08:29 AM   #1
ashevillan
LQ Newbie
 
Registered: Mar 2013
Posts: 5

Rep: Reputation: Disabled
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>
 
Old 05-11-2013, 10:03 AM   #2
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Rep: Reputation: 23
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>
 
Old 05-12-2013, 01:01 AM   #3
ashevillan
LQ Newbie
 
Registered: Mar 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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...
 
Old 05-12-2013, 05:34 AM   #4
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Rep: Reputation: 23
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];

Last edited by kedarp; 05-12-2013 at 05:36 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Strange Result with sed and Array. mrm5102 Programming 8 05-18-2012 02:15 PM
[SOLVED] Bash: Calculating in array and storing result in array ghantauke Linux - Newbie 6 12-02-2010 12:28 PM
cURL php upload posting $_FILES[file][name] array creation devwink Programming 2 04-16-2010 07:19 AM
problem exporting result of awk to an array... mitramcc Linux - Newbie 2 02-10-2009 11:47 AM
Storing the result of execvp to a char array in C kponenation Programming 3 12-14-2005 05:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration