Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

forms - Getting value of HTML text input

Say I have an HTML form like this to collect an email from a website visitor:

<form name="input" action="handle_email.php" method="post">
Email: <input type="text" name="email" />
<input type="submit" value="Newsletter" />
</form> 

Now when the submit button is pressed I can get the value of the email field in handle_email.php by using $_POST["email"]. But say I'd like to use the value of the email text input elsewhere. Is it possible to get the email value "outside" of the form processing?

UPDATE: Thanks to everyone for their quick replies. What I'm trying to do is get the email box to do double-duty. For example, I've renamed the submit button to "Newsletter" in the above code. When that button is pressed the handle_email.php script will just sign up the user for a newsletter. However, I also have a "Register" link on the same page that redirects the visitor to a registration form where they can enter lots of other info if they like. Now, if the user happened to have entered data in the email text box I'd like to send it along to the registration page so that field can be pre-populated. For example:

<a href="http://mywebsite.com/register?user_email="[how can I put the email here?]"/>Register</a>

My apologies for not being clear enough in my original post.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use the value of the email input somewhere else on the same page, for example to do some sort of validation, you could use JavaScript. First I would assign an "id" attribute to your email textbox:

<input type="text" name="email" id="email"/>

and then I would retrieve the value with JavaScript:

var email = document.getElementById('email').value;

From there, you can do additional processing on the value of 'email'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...