Triforce
Triforce

Reputation: 11

PHP: Redirect to another page after HTML form executes a php script (wordpress)

I got a HTML form on a wordpress website which executes a PHP script after submitting. That works fine, but after the PHP script is executed I need to redirect to another wordpress page, does anyone knows how I could do that?

Here's the HTML code on my Wordpress page:

<form action="[insert_php] include('wp-content/logindataupload.php');         
[/insert_php]" method="post" >First name
<input name="field1" type="text" />
Name
<input name="field2" type="text" />
E-Mail
<input name="field3" type="text" />

<input type="submit" value="Submit" /></form>

And here's the code in the "logindataupload.php" file:

<?php
 $path = 'wp-content/uploadhistory.txt';

 if (isset($_POST['field1']) && isset($_POST['field2']) && isset($_POST['field3'])) {
    $fh = fopen($path,"a+");
    $string = $_POST['field1'].$_POST['field2'].'_'.$_POST['field3']."\n";
    fwrite($fh,$string); // Write information to the file
    fclose($fh); // Close the file
 }

 $path2 = 'wp-content/currentupload.txt';

 file_put_contents($path2, "");

 if (isset($_POST['field1']) && isset($_POST['field2']) && isset($_POST['field3'])) {
    $fh = fopen($path2,"a+");
    $string = $_POST['field1'].$_POST['field2'].'_'.$_POST['field3'];
    fwrite($fh,$string); // Write information to the file
    fclose($fh); // Close the file
 }

?>

I'm quite new into PHP and I think I have to do the redirect in the "logindataupload.php" file. I already tried to insert the following behind the last "?>"

header("Location: http://page-i-want-to-redirect-to.com"); /* Redirect browser */

If something's missing, please tell me. Thank you very much.

Upvotes: 0

Views: 409

Answers (2)

symcbean
symcbean

Reputation: 48387

This should be a comment, but its a bit long.

does anyone knows how I could do that?

Yes - and your question should be closed as a duplicate of this one or this one or many others here on SO.

I'm quite new into PHP

By the look of things you are new to computers and programming. Writing to flat files on a multi-user system is not a recipe for a successful outcome.

Copying the same blocks of code and injecting different variable settings in between is not good programming practice.

Upvotes: 0

Mohammad hayajneh
Mohammad hayajneh

Reputation: 674

if (isset($_POST['field1']) && isset($_POST['field2']) && isset($_POST['field3'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'].$_POST['field2'].'_'.$_POST['field3']."\n";
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
header("Location: logindataupload.php");
exit();
}

use the header function like this if this didnt work just add ob_start(); at the beginning of the page

Upvotes: 1

Related Questions