我已多次检查代码,但无法找到错误! issetCookie在这里返回false。
<html>
<body>
<?php if(!isset($_REQUEST['sub'])) { ?>
<form name="f1" action="1.php" method="post">
Name : <input type="text" name="na"><br>
Last Name : <input type="text" name="lna"><br>
Email Id : <input type="text" name="eml"><br>
Phone No : <input type="text" name="phn"><br>
City : <input type="text" name="cty"><br>
<input type="Submit" name="sub" value="ok"><input type="Reset" name="res" value="Clear">
<?php } else {
$name=$_REQUEST['na'];
$lname=$_REQUEST['lna'];
$email=$_REQUEST['eml'];
$phone=$_REQUEST['phn'];
$city=$_REQUEST['cty'];
setcookie("Name", $name, time()+3600, "/","", 0);
setcookie("LName",$lname);
setcookie("Email",$email);
setcookie("Phone",$phone);
setcookie("City",$city);
} ?>
</form>
</body>
</html>
有人可以帮我为Cookie分配变量吗? 如果是else的问题,请建议使用表单数据分配cookie值并在表单操作中定义的下一页上检索的备用选项。
答案 0 :(得分:0)
因为你试图在else
条件中设置cookie。所以变量在else条件中不存在,因为表单未提交。你需要像这样添加过期时间。
setcookie("Name", $name, time() + (86400 * 30), "/");// one day
并像这样检查
if(!isset($_COOKIE['Name'])) {
echo "Cookie named is not set!";
} else {
echo "Cookie is set!<br>";
echo "Value is: " . $_COOKIE['Name'];
}
你的代码应该是这样的
<?php
if (isset($_POST['sub'])) {
$name=$_POST['na'];
$lname=$_POST['lna'];
$email=$_POST['eml'];
$phone=$_POST['phn'];
$city=$_POST['cty'];
setcookie("Name", $name, time() + (86400 * 30), "/");
}
if(!isset($_COOKIE['Name'])) {
echo "Cookie named is not set!";
} else {
echo "Cookie is set!<br>";
echo "Value is: " . $_COOKIE['Name'];
}
?>
<html>
<body>
<form name="f1" action="1.php" method="post">
Name : <input type="text" name="na"><br>
Last Name : <input type="text" name="lna"><br>
Email Id : <input type="text" name="eml"><br>
Phone No : <input type="text" name="phn"><br>
City : <input type="text" name="cty"><br>
<input type="Submit" name="sub" value="ok"><input type="Reset"
name="res"
value="Clear">
</form>
</body>
</html>
答案 1 :(得分:0)
像这样使用。
public class News_Yeb extends Fragment implements OnClickListener {
String DataParseUrl = "http://fitandfineindustries.com.np/insert.php" ;
Boolean CheckEditText ;
private static View view;
EditText name,email,contact ;
String Getname,GetEmail,GetContact ;
private static Button signUpButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.news_yeb, container, false);
initViews();
setListeners();
return view;
}
// Initialize all views
private void initViews() {
name = (EditText) view.findViewById(R.id.fullName);
email = (EditText) view.findViewById(R.id.userEmailId);
contact = (EditText) view.findViewById(R.id.mobileNumber);
signUpButton = (Button) view.findViewById(R.id.signUpBtn);
}
// Set Listeners
private void setListeners() {
signUpButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
GetCheckEditTextIsEmptyOrNot();
if(CheckEditText){
SendDataToServer(Getname, GetEmail, GetContact);
}
else {
Toast.makeText(getActivity(), "Please fill all form fields.", Toast.LENGTH_LONG).show();
}
}
// Check Validation Method
private void GetCheckEditTextIsEmptyOrNot() {
// Get all edittext texts
String Getname = name.getText().toString();
String GetEmail = email.getText().toString();
String GetContact = contact.getText().toString();
if(TextUtils.isEmpty(Getname) || TextUtils.isEmpty(GetEmail) || TextUtils.isEmpty(GetContact))
{
CheckEditText = false;
}
else {
CheckEditText = true ;
}
}
public void SendDataToServer(final String name, final String email, final String contact){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String QuickName = name ;
String QuickEmail = email ;
String QuickContact = contact;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", QuickName));
nameValuePairs.add(new BasicNameValuePair("email", QuickEmail));
nameValuePairs.add(new BasicNameValuePair("contact", QuickContact));
try {
HttpPost httpPost = new HttpPost(DataParseUrl);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
URL url = new URL("http://fitandfineindustries.com.np/insert.php");
URLConnection urlConnection = (HttpURLConnection) url.openConnection();
} catch (ClientProtocolException e) {
} catch (IOException e) {
return new String("Exception: " + e.getMessage());
}
return "Data Submit Successfully";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getActivity(), "Data Submitted Successfully.", Toast.LENGTH_LONG).show();
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, email, contact);
}
}