/    Sign up×
Community /Pin to ProfileBookmark

Which form atts can I use with PHP?

Hello all!

I am working on developing a form and I am wondering which form attributes I can use with PHP. I tried to use a multiple selection option as I will show below:

What equipment do you currently own?<br />
<select name=”equipment” size=”4″ multiple=”multiple”>
<option>Mask</option>
<option>Snorkel</option>
<option>Fins</option>
<option>Wetsuit</option>
<option>Regulator</option>
<option>BCD</option>
<option>Weight belt</option>
</select>

Then I tried to use the follwing code to output the options selected:

You own <?php echo $_POST[‘equipment’]; ?>.

I do get one of the options but not all of them. Can I use this option with PHP or am I using the wrong PHP code to output the info.

Also is there some way to output multiple items using one line of code such as: first_name and last_name are seperate items that I want to print out one after the other. Do I need to use two seperate sections of code?

Hi <?php echo $_POST[‘first_name last_name’]; ?>.

Thank you for helping,
Robbie

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@Paul_JrOct 14.2004 — [font=palatino linotype]If you want to capture all of the selected options, you need to change the name of the select just slightly &#8212; you will have to add &#8220;[]&#8221; to the end of the name. Doing this will cause PHP to treat the select as an array, with the array elements being the options you chose.[/font]
<i>
</i>&lt;select name="equipment[]" size="4" multiple="multiple"&gt;
&lt;option&gt;Mask&lt;/option&gt;
&lt;option&gt;Snorkel&lt;/option&gt;
&lt;option&gt;Fins&lt;/option&gt;
&lt;option&gt;Wetsuit&lt;/option&gt;
&lt;option&gt;Regulator&lt;/option&gt;
&lt;option&gt;BCD&lt;/option&gt;
&lt;option&gt;Weight belt&lt;/option&gt;
&lt;/select&gt;

[font=palatino linotype]Now [font=courier]$_POST['equipment'][/font] is an array containing the options you selected.[/font]



[font=palatino linotype]Using this sort of notation &#8212; [font=courier]<?php echo $_POST['first_name last_name']; ?>[/font] &#8212; is incorrect. You have to echo them out separately, like this:[/font]

[code=php]
<?php
echo $_POST['first_name'] . ' ' . $_POST['last_name'];
// Or
echo $_POST['first_name'] . ' ';
echo $_POST['last_name'];
?>
[/code]

[font=palatino linotype]The point is that you cannot echo out more than one array element just by specifying more than one name inside the square brackets.[/font]
Copy linkTweet thisAlerts:
@rch10007authorOct 15.2004 — I will try this, I'm sure it will work.

Thanks for helping,

Robbie
Copy linkTweet thisAlerts:
@Paul_JrOct 15.2004 — [font=palatino linotype]You&#8217;re welcome. ?[/font]
Copy linkTweet thisAlerts:
@rch10007authorOct 15.2004 — I tried it. This is what I put in:

What equipment do you currently own?<br />

<select name="equipment[]" size="4" multiple="multiple">

<option>Mask</option>

<option>Snorkel</option>

<option>Fins</option>

<option>Wetsuit</option>

<option>Regulator</option>

<option>BCD</option>

<option>Weight belt</option>

</select>

The output I get is:

You own Array.

from the code:

You own <?php echo $_POST['equipment']; ?>.

The URL is: http://www.childsinc.com/php/idea_form.php

Thanks,

Robbie
Copy linkTweet thisAlerts:
@rch10007authorOct 15.2004 — I have another problem outputing the result from another multiple selection:

Are there specialty courses that you are interested in?<br />

<input type="checkbox" name="specialty" value="photography" />Photography<br />

<input type="checkbox" name="specialty" value="deep" />Deep Diver<br />

<input type="checkbox" name="specialty" value="nitrox" />Nitrox<br />

<input type="checkbox" name="specialty" value="firstaid_cpr" />First Aid &amp; CPR<br />

<input type="checkbox" name="specialty" value="drysuit" />Dry-suit Training<br />

<input type="checkbox" name="specialty" value="low_vis" />Low Visibility/Navagation

This will only return one value using the following code:

You are interested in <?php echo $_POST['specialty']; ?>.

How do I make an array out the checkboxes, or can I?

Thanks again,

Robbie
Copy linkTweet thisAlerts:
@MstrBobOct 15.2004 — (1) When you have a select, the user chooses ONE option out of many. Hence, when the code submits, you should only get one option.

(2) When using the Option tags, one should include the value with the value attribute. Although the form can capture the value by what's between <option> and </option> it's a much better idea to use the value tag:

<i>
</i>&lt;select name="equipment" size="4" multiple="multiple"&gt;
&lt;option value="Mask"&gt;Mask&lt;/option&gt;
&lt;option value="Snorkel"&gt;Snorkel&lt;/option&gt;
&lt;option value="Fins"&gt;Fins&lt;/option&gt;
&lt;option value="Wetsuit"&gt;Wetsuit&lt;/option&gt;
&lt;option value="Regulator"&gt;Regulator&lt;/option&gt;
&lt;option value="BCD"&gt;BCD&lt;/option&gt;
&lt;option value="Weight Belt"&gt;Weight belt&lt;/option&gt;
&lt;/select&gt;


(3) You shouldn't have multiple checkboxes with the same name.
Copy linkTweet thisAlerts:
@rch10007authorOct 15.2004 — More than one option can be selected if you hold the mouse down and drag over all of them.
Copy linkTweet thisAlerts:
@MstrBobOct 15.2004 — Sorry, I didn't see the multiple. This option, however, doesn't allow you to select certain ones. It must be in order. What if I have a Mask and Fins, but no snorkel ? This goes in order, so, if I select up to Fins, you know that I also have Mask and Snorkel selected. This isn't passed in the post array. With checkmarks, if you have multiple with the same name, only the last one will be passed. You must give each different names.
Copy linkTweet thisAlerts:
@rch10007authorOct 15.2004 — you can hold down the ctrl button and select
Copy linkTweet thisAlerts:
@MstrBobOct 15.2004 — Except, the Post array will only contain the last selected option.
Copy linkTweet thisAlerts:
@rch10007authorOct 15.2004 — What all can I do with the forms and PHP? I know there has to be some sort of structure and limitaions but i can find any references on the internet.

The form I wrote has all the ways you can offer form selections and I am trying to learn how to get them all to work with PHP, but I can't.

If you could point me in the direction of where I can find info about forms and PHP I would greatly appreciate it OR if you can help me work this out, that will be great too.

Thanks for the help,

Robbie
Copy linkTweet thisAlerts:
@MstrBobOct 15.2004 — I'm sorry, I must retract my original statement. Paul is right, adding [] after the name, and calling it by $_POST['equipment'] will return an array of the values. However, this action isn't as well known to users, and I'd suggest informing them of this, or using checkboxes. Once again, my apologies.

EDIT: The same also applies to checkboxes. add [] to the end of the name, and you can call the array (minus the [] )
Copy linkTweet thisAlerts:
@rch10007authorOct 15.2004 — I am just trying to master all the ways to offer form selections using PHP. ?

There is so much to learn, I thought I would start there.

I will try to work on the other selection, the boxes, and see what I can do, but I'm a noob.

Thanks,

Robbie
Copy linkTweet thisAlerts:
@MstrBobOct 15.2004 — Here:

[code=php]
<?PHP
if(empty($_POST['specialty']))
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Content-Style-Type" content="text/css" />
<meta http-equiv="content-script-type" content="text/javascript" />
<title></title>
</head>
<body>
<form action="<?PHP echo($_SERVER['PHP_SELF']); ?>" method="post">
<fieldset>
<input type="checkbox" name="specialty[]" value="photography" />Photography<br />
<input type="checkbox" name="specialty[]" value="deep" />Deep Diver<br />
<input type="checkbox" name="specialty[]" value="nitrox" />Nitrox<br />
<input type="checkbox" name="specialty[]" value="firstaid_cpr" />First Aid & CPR<br />
<input type="checkbox" name="specialty[]" value="drysuit" />Dry-suit Training<br />
<input type="checkbox" name="specialty[]" value="low_vis" />Low Visibility/Navagation
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>
<?PHP
} else {
print_r($_POST['specialty']);
}
?>
[/code]
×

Success!

Help @rch10007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...