0

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.

  • _behind the last "?>"_ - well since it is a PHP function, it would obviously belong _into_ `` tags, otherwise it is just _text_ ... – CBroe Nov 01 '17 at 13:30
  • sorry I wrote wrong... I mean I inserted it before the las "?>"... – Triforce Nov 01 '17 at 13:40
  • Well then your question is lacking the actual _problem_ description. Please go read [ask]. – CBroe Nov 01 '17 at 13:41

2 Answers2

1
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

Mohammad hayajneh
  • 623
  • 2
  • 11
  • 32
  • Thanks for the fast answer, I tried this (both, with and without the "ob_start();") but then after submitting I just get a plank page... – Triforce Nov 01 '17 at 13:48
0

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.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • yes sorry, I know I'm not that good at programming... but I try it hard to get into it... And it's not exactly the same question as those you show me, because I have that script inbetween, I saw these other posts, but I couldn't fix my issue with theese... – Triforce Nov 01 '17 at 13:50