0

I've been trying to make a form in Wordpress to post data to the database and check if the data matches and if so then it proceeds to the next form but I can't seem to get it to work properly, been testing having the form placed in the themes folder to allow straighter testing, the php looks like this:

<?php

if(isset($_POST['submit'])) {
  global $wpdb;

  $ordernumber = $_POST['ordernmbr'];
  $orderfirstname = $_POST['firstname'];
  $orderpostnumber = $_POST['postnmbr'];
  // Sanitizing
  $ordernumber = stripslashes_deep($ordernumber);
  $orderfirstname = stripslashes_deep($orderfirstname);
  $orderpostnumber = stripslashes_deep($orderpostnumber);


  $sql = "SELECT * FROM wp_postmeta WHERE 'post_id' = %d";
  $sql = $wpdb->prepare($sql, array($ordernumber));
  $res = $wpdb->get_results($sql);

  if ($res > 0) {
  wp_redirect(admin_url('http://localhost/wordpress/index.php/shop/'));
  die();

} else {
  $error = "Not like this";
  echo $error;
}
  print_r($res);
}
?>
<?php
 get_footer();
?>

    }

The problem is that when I try to post data it gives me an error saying

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-includes/class.wp-styles.php:225) in /Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-includes/pluggable.php on line 1219

how do I go on about fixing this?

Veraen
  • 65
  • 2
  • 13
  • move the POST code at top in header file – Vel Sep 05 '18 at 10:35
  • _“been testing having the form placed in the themes folder to allow straighter testing”_ - meaning what exactly? Are you using this as an actual template file? That’s not where data _processing_ logic should be put in the first place. – misorude Sep 05 '18 at 10:36
  • @misorude originally this was supposed to be a plugin but for now I haven't been able to make it show as such so I tried getting it to show on an actual page by adding it as a template within the theme itself and linking it that way. – Veraen Sep 05 '18 at 10:37
  • @Vel thanks that worked – Veraen Sep 05 '18 at 10:42

1 Answers1

0

You can't redirect the browser after the content has already begun being served.

That piece of code needs to be included before the theme starts loading, so it needs to be in a plugin.

When setting up the plugin, be sure to give it a high priority, and choose the right filter/action to have it load early (before any content is rendered): WP Action Reference

Sergio S.
  • 116
  • 5