Fill a PHP/HTML field in a website

ε祈祈猫儿з 提交于 2019-12-25 01:55:52

问题


I want to automatically fill the field called "email" of a webpage thesite.com/email.php, which code is something similar to this:

<input type="text" name="email" value="" size="24">
<br> <input type="submit" name="submit" value="Send">

And then, after filling the field, I also would like to perform the action "submit". But I don't know actually how to do that with Java Can someone help me? Thanks a lot.


回答1:


To programmatically submit the form with java, you don't directly fill the form, rather send the form information to the submit page via HTTP GET or POST. You did not provide the onsubmit value in your post, but you would use that webpage URL and send the form information via a URLConnection. If using GET, you send the data in a query string (where key/value are the form parameters):

URL url = new URL("http://mywebsite/form-submit-webpage.php?key1=value1&key2=value2");

If POST, you must use the OutputStream of URL connection to set the POST key/value pairs

URL url = new URL("http://mywebsite/form-submit-webpage.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
//write key value pairs to os. 

From their, get get the InputStream from the URLConnection to read the results. See https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html




回答2:


What you need to do is create a form in html and a form handler in php.

HTML code posting the information to "welcome.php"

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

welcome.php can handle the variables in different ways. Here is an example:

<html>
<body>

    Welcome <?php echo $_POST["name"]; ?><br>
    Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

The file will pass the variables by their names. In this example, the names are "name" and "email" using the post method. In the php file, you receive the variables useing the $_POST method.



来源:https://stackoverflow.com/questions/29286289/fill-a-php-html-field-in-a-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!