/    Sign up×
Community /Pin to ProfileBookmark

text box value.

I’m just wondering how I get the value of a text box which has the same name as the paragraph it is in.

In javascript I used:

[CODE]document.forms[0].UserInfo.value [/CODE]

but in php I tried:

[CODE]<?php echo $_POST[‘UserInfo’]; ?>[/CODE]

to get the value and display it as text.

I’m guessing it is because the html looks like this:

[CODE]<p id=”UserInfo”>What is your name?</p>
<div>
<input type=”text” name=”UserInfo” size=”40″ />
</div>[/CODE]

The HTML is all in one form and I do not have access to it to change it.

Any help would be appreciated.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 07.2011 — Works OK for me:
[code=php]
<html><head><title>test</title></head><body>
<form action='' method='post'>
<p id="UserInfo">What is your name?</p>
<div>
<input type="text" name="UserInfo" size="40" />
</div>
<p><input type='submit' /></p>
</form>
<?php
if(!empty($_POST)) {
echo "<pre>".print_r($_POST, true)."</pre>";
}
?>
</body></html>
[/code]
Copy linkTweet thisAlerts:
@xcampkillxauthorMar 08.2011 — what if there is just the one form but it has lots of selectable values in it? Check boxes, radio buttons, the text boxes. How do I distinguish between them?
Copy linkTweet thisAlerts:
@eval_BadCode_Mar 08.2011 — You distinguish between them by looking at the name. That is all there is to it. You can submit the form many ways I suppose, but in general most people do not mess with javascript when the traditional "submit" button gets from point A to point B faster and cleaner.

[code=php]
<?php
$types = array("checkbox","radio","text");
?>

<form target="_self" method="post">
<input name="foo" type="<?php echo $types[array_rand($types)]; ?>">
<input name="bar" type="<?php echo $types[array_rand($types)]; ?>">
<input name="baz" type="<?php echo $types[array_rand($types)]; ?>">
<button type="submit" name="submit">Try it</button>

<br /><br />
<?php echo "<pre>", print_r($_POST), "</pre>"; ?>

[/code]


Just keep trying this sample script... eventually it'll click (after 2 or 3 goes).
×

Success!

Help @xcampkillx 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...