我最近为我的网页制作了一个表单,但我不断收到来自表单的电子邮件。即使我没有发送表格,我也会收到电子邮件。
如果我提交表格,我会收到2或3封电子邮件。 1表示我的表格信息,2表示没有信息。
php代码:
<form action="" method="POST">
<em>Last name:</em></br>
<select name="title" id="title">
<option>Mr.</option>
<option>Mrs.</option>
<option>Dr.</option>
</select>
<input type="text" size="25" name="name" id="name" placeholder="Last name" required="required"><p /><br>
<em>company name:</em></br>
<input type="text" size="25" name="companyname" id="companyname" placeholder="Company Name" required="required"><p /><br>
<em>Email:</em></br>
<input type="text" size="25" name="mail" id="mail" placeholder="email" required="required"><p /><br>
<em>how many extra pages:</em><br>
<select name="extrapages" id="extrapages">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select><br><br>
<em>Would you like us to put your site on the internet?</em><br>
<input type="radio" name="sow" id="sow" value="Yes" required="required">Yes
<input type="radio" name="sow" id="sow" value="No" required="required">No<br><br>
<input type="Submit" value="Send!" name="submit">
</form>
html代码:
public class MainActivity extends AppCompatActivity
{
EditText userName;
EditText password;
Button sButton;
HttpClient httpClient;
HttpPost httpPost;
HttpResponse httpResponse;
String username;
String pass;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText)findViewById(R.id.user_id);
password = (EditText) findViewById(R.id.user_password);
sButton= (Button) findViewById(R.id.s_button);
username = userName.getText().toString();
pass = password.getText().toString();
httpClient = new DefaultHttpClient();
httpPost = new HttpPost("192.168.100.106/EMS/functions.php");
final JSONObject jsonObject = new JSONObject();
sButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Thread thread = new Thread()
{
@Override
public void run()
{
try
{
jsonObject.put("username", username);
jsonObject.put("password", pass);
Log.wtf("Sent data :","username and password");
httpPost.setEntity(new StringEntity(jsonObject.toString()));
}
catch(JSONException | UnsupportedEncodingException e)
{
e.printStackTrace();
}
try {
httpResponse = httpClient.execute(httpPost);
String str = EntityUtils.toString(httpResponse.getEntity());
JSONObject responseObject = new JSONObject(str);
String response = responseObject.getString("success");
if(response.equals("1"))
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Credentials match successful.",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),index.class);
startActivity(intent);
}
});
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
};thread.start();
}
});
}
}
答案 0 :(得分:1)
因为您的表单设置为每次加载页面时都运行 。您不检查是否提交表单。您还绝对没有验证。
要修复此包装,请将所有此代码包装在if语句中,以检查表单是否已提交。您可以查看包含$_SERVER
超级全局和名为REQUEST_METHOD
的密钥,它将告诉您页面是否是通过POST请求的(通常在提交表单时)或GET(在“典型的“页面加载”。如果它的值是“POST”,则表单已提交,您可以处理数据,如果没有,则忽略该代码。
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// your code goes here
}