我是PHP的新手,我正在尝试创建一个表单,当用户填写时,会重定向到我的成功页面,然后启动下载并将其详细信息保存到.txt文件中。
我已设法完成上述所有操作,但未重定向用户,但仍会运行下载文件的代码(位于成功页面中)。
为什么会这样?
如果我删除了下载文件的代码,那么用户被重定向,我是否需要在文件下载之前添加延迟?
的index.php
public class SplashScreen extends Activity implements View.OnClickListener, ResultCallback<Status>,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
TextView miles, push, alarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
buildGoogleApiClient();
System.out.println("Building Google Api Client");
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext())
!= ConnectionResult.SUCCESS) {
//TODO Add something to post a message to the screen that the service isn't available
System.out.println("Service not Available");
} else {
System.out.println("Service Available");
miles = (TextView) findViewById(R.id.tvmiles);
push = (TextView) findViewById(R.id.tvpush);
alarm = (TextView) findViewById(R.id.tvalarm);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
}
ArrayList geoFenceList;
PendingIntent geoFencePendingIntent = null;
/**
* Provides the entry point to Google Play services.
*/
protected GoogleApiClient mGoogleApiClient;
@Override
public void onClick(View v) {
System.out.println("Onclick pressed");
DataGather data = new DataGather();
data.writePreferences(this, 5, false, true);
data.writeRecentJourney(this, "Wareham", "Poole");
double longitude = data.getGoingToLong(getBaseContext());
double latitude = data.getGoingToLat(getBaseContext());
float proximity = getProximity();
/*
* Add the geofence work.
*/
geoFenceList = new ArrayList();
geoFenceList.add(
new Geofence.Builder()
.setRequestId("Testing the addition")
.setCircularRegion(
latitude,
longitude,
proximity)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.build()
);
System.out.println("Added Geofence to Arraylist");
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(this);
System.out.println("Added GeoFence");
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
/**
* Builds a GoogleApiClient. Uses the {@code #addApi} method to request the LocationServices API.
*/
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder geoFencingRequestBuilder = new GeofencingRequest.Builder();
geoFencingRequestBuilder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
geoFencingRequestBuilder.addGeofences(geoFenceList);
System.out.println("Returning geoFencingRequestBuilder.build()");
return geoFencingRequestBuilder.build();
}
/**
* Uses the users inserted information to derive the proximity setting.
* @return Returns a float of the proximity number.
*/
private float getProximity(){
DataGather dataGather = new DataGather();
String[] prefs = dataGather.getPreferences(this);
System.out.println("Returning Float" + Float.parseFloat(prefs[0]) * 1609.344);
return (float) (Float.parseFloat(prefs[0]) * 1609.344);
}
private PendingIntent getGeofencePendingIntent(){
Intent intent = new Intent(this, TraiNapIntentService.class);
System.out.println("Returning PendingIntent.getSerivce");
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
/**
* TODO Finish this method
* @param status
*/
public void onResult(Status status) {
if (status.isSuccess()) {
System.out.println("Success status received");
} else {
System.out.println("Non-Success status received");
}
}
/**
* TODO Finish this method
* @param bundle
*/
@Override
public void onConnected(Bundle bundle) {
System.out.println("onConnected");
}
/**
* TODO Finish this method
* @param i
*/
@Override
public void onConnectionSuspended(int i) {
System.out.println("onConnectionSuspended");
}
/**
* TODO Finish this method
* @param connectionResult
*/
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
System.out.println("Connection Failed");
}
}
myform.php
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form action="myform.php" method="post">
Name <input type="text" name="name" value="">
Company <input type="text" name="company" value="">
Website <input type="text" name="website" value="">
Email <input type="text" name="email" value="">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
success.php
<?php
$name = @$_POST['name'];
$company = @$_POST['company'];
$website = @$_POST['website'];
$email = @$_POST['email'];
$filename = "info.txt";
$f_data= '
Name : '.$name.'
Email : '.$email.'
Website: '.$website.'
Company: '.$company.'
============================================================================= =
';
$file = fopen($filename, "a");
fwrite($file,$f_data);
fclose($file);
header("Location:success.php");
?>
答案 0 :(得分:0)
success.php
是一个下载。您的浏览器不会重定向到下载,它只会下载它。您需要将下载代码移动到另一个文件 - 比如download.php
,然后重定向到该文件。拥有“下载报告”按钮而不是自动下载可能是更好的用户体验。