Jump to content

Array from a Form


cpsc_girl

Recommended Posts

I was trying to create a form that uses text boxes to input numeric values, then add the values to an array. Then print a statement with the sorted values, but i'm completely lost. I created the form which i believe is right:



<html>
<head><title>Average and Median values</title></head>
<body>

<h1>Value Finder</h1>

<font size=4><p>Please enter up to 8 numbers in the form below.</p>

<form action="array_form.php" method="post">

Value 1 : <input type="text" size="5" maxlength="10" name="value">
<br>Value 2 : <input type="text" size="5" maxlength="10" name="value">
<br>Value 3 : <input type="text" size="5" maxlength="10" name="value">
<br>Value 4 : <input type="text" size="5" maxlength="10" name="value">
<br>Value 5 : <input type="text" size="5" maxlength="10" name="value">
<br>Value 6 : <input type="text" size="5" maxlength="10" name="value">
<br>Value 7 : <input type="text" size="5" maxlength="10" name="value">
<br>Value 8 : <input type="text" size="5" maxlength="10" name="value">

<br><br><input type="submit" value="Submit">
<input type="reset" value="Clear">

</font>
</form>
</body>
</html>



The part that i'm lost on is how to get the numbers in the array from the form. The book i'm using to teach myself is no help at all. Someone please help me.

On the server that i'm using the registerd globlas are turned off.

Link to comment
Share on other sites

So the code should look like this instead, i was sure if i had to include the brackets or not.



<html>
<head><title>Average and Median values</title></head>
<body>

<h1>Value Finder</h1>

<font size=4><p>Please enter up to 8 numbers in the form below.</p>

<form action="array_form.php" method="post">

Value 1 : <input type="text" size="5" maxlength="10" name="value[]">
Value 2 : <input type="text" size="5" maxlength="10" name="value[]">
Value 3 : <input type="text" size="5" maxlength="10" name="value[]">
Value 4 : <input type="text" size="5" maxlength="10" name="value[]">
Value 5 : <input type="text" size="5" maxlength="10" name="value[]">
Value 6 : <input type="text" size="5" maxlength="10" name="value[]">
Value 7 : <input type="text" size="5" maxlength="10" name="value[]">
Value 8 : <input type="text" size="5" maxlength="10" name="value[]">

<br><br><input type="submit" value="Submit">
<input type="reset" value="Clear">

</font>
</form>
</body>
</html>

I'm still stuck on creating an array from the numbers input in the form, for some reason i can't get the array to work at all.


<?php

    $value = $_POST["value"];


    $numb = array($value);

print ("$numb");
   
?>

I couldn't find any examples comparative to this particular problem.
Link to comment
Share on other sites

Thanks. I tried the php code you just posted but i'm still receiving an error:

Warning: Invalid argument supplied for foreach() in /usr/local/www/cpsc/students/thamp815/array_form.php on line 5


Also what happens to the array isn't that required if you're trying to create an array?
Link to comment
Share on other sites

Yu dont need to define an array as the form creates an array for you when yoiu use the value[] method.

Your form. You need to move your </font> tag to be after your </form> tag. The rest is fine.

The rest of your code should be working fine though you will get the above metntioned error if none of your form fileds have been filled in. Try...

[code]
<?php
if (isset($_POST['value'] && is_array($_POST['value'])) {
  foreach($_POST['value'] as $numb) {
    echo $numb;
  }
} else {
// do some debugging.
print_r($_POST);
}
?>
[/code]
Link to comment
Share on other sites

Then the conditions in the if statement are not getting set and you are seeing the post data in the else branch. There must be a mistake somewhere.

Copy and paste this entire code into a page and it should work for you:
[code]<html>
<head><title>Average and Median values</title></head>
<body>

<?php

if (isset($_POST['value'])) {
   echo "<pre>";
   print_r($_POST[value]);
   echo "</pre>";
}

?>


<h1>Value Finder</h1>

<font size=4><p>Please enter up to 8 numbers in the form below.</p>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">

Value 1 : <input type="text" size="5" maxlength="10" name="value[]"><br>
Value 2 : <input type="text" size="5" maxlength="10" name="value[]"><br>
Value 3 : <input type="text" size="5" maxlength="10" name="value[]"><br>
Value 4 : <input type="text" size="5" maxlength="10" name="value[]"><br>
Value 5 : <input type="text" size="5" maxlength="10" name="value[]"><br>
Value 6 : <input type="text" size="5" maxlength="10" name="value[]"><br>
Value 7 : <input type="text" size="5" maxlength="10" name="value[]"><br>
Value 8 : <input type="text" size="5" maxlength="10" name="value[]"><br>

<input type="submit" value="Submit">
<input type="reset" value="Clear">

</form>
</font>
</body>
</html>[/code]

EDIT: It also works with the font tag out of place, so the error is somewhere else.
Link to comment
Share on other sites

[quote]Is there a simplier way to write an array?[/quote]

Of course, but it really depends what you want to use it for. A few examples...

[code]
<?php
  $arrayofnames = array('bob','betty','bill');
  foreach($arrayofnames as $name) {
    echo $name.'<br />';
  }
?>
[/code]

or...

[code]
<?php
  $arrayofnumber = array(1,5,66,33,10);
  foreach($arrayofnumbers as $number) {
    echo $number.'<br />';
  }
?>
[/code]
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.