Submit button works on local host but not fully working on live server

Im working on a site, writing in php, with minimal js and css.
It works okay on local host, but having problems with submit buttons when the site is uploaded to the live server.

My site has a few databases

On the live server, the submit button is not returning to the original page, but it is submitting the users inputed data to the database.

The basic structure of my site ;
index.php
about.php
[includes] header.php, footer.php, menu.php
[crud] index.php, create.php
[core] databased.php

In ‘about.php’ I insert a link to ‘index.php’ in a folder named 'crud ’

<?php include 'crud/index.php';?>

This index.php page initially displays a database of names, email, mobile. There is a create button to enter new customers details, which calls the create.php file

<p>
   <a href="crud/create.php" class="btn btn-success">Create</a>
</p>

When you click on the Create button a standalone page is displayed without the standard header and footer
Here you enter new customers details, there is also a create button here, and on the local host when I press this button,
it returns me to the about.php page with the new data visable in the about.php page

So on the live site,
Im on the about page, click on create,
takes me to
websiteaddress/crud/create.php
enter data click on second create button
but it just stays on this page…

here is the code I fear could be the problem

in the create.php file

	// insert data
	if ($valid) {
		$pdo = databased::connect();
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$sql = "INSERT INTO customers (name,email,mobile) values(?, ?, ?)";
		$q = $pdo->prepare($sql);
		$q->execute(array($name,$email,$mobile));
		databased::disconnect();
		header("Location: ../about.php");

I think the issue could be the header, as on localhost this directory Location: …/about.php works, but maybe it needs to be more specified on a liver server?

When Im on the create page
crud/create.php
and I click the create button
the data is added to the database on the live server, but this action should also return me to the about page

about.php .but it doesnt it just seems to hang and get stuck on crud/create.php but every time you click the create button as the page doesnt refresh the data you entered is still on the form, and it is added as a duplicate.

Many thanks if anyone can help

The header() statement is probably failing due to some output being sent to the browser. To determine if this is the case, temporarily add the following statements immediately after the first opening <?php tag in the crud/create.php file -

ini_set('display_errors', '1');
error_reporting(-1);
1 Like

Many thanks for your response
It says
" Warning : Cannot modify header information - headers already sent by (output started at /homepages/40/d835959195/htdocs/crud/create.php:1) in /homepages/40/d835959195/htdocs/crud/create.php on line 49"

This is the first 50 lines…


<?php 
	ini_set('display_errors', '1');
error_reporting(-1);

	require '../core/databased.php';

	if ( !empty($_POST)) {
		// keep track validation errors
		$nameError = null;
		$emailError = null;
		$mobileError = null;
		
		// keep track post values
		$name = $_POST['name'];
		$email = $_POST['email'];
		$mobile = $_POST['mobile'];
		
		// validate input
		$valid = true;
		if (empty($name)) {
			$nameError = 'Please enter Name';
			$valid = false;
		}
		
		if (empty($email)) {
			$emailError = 'Please enter Email Address';
			$valid = false;
		} else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
			$emailError = 'Please enter a valid Email Address';
			$valid = false;
		}
		
		if (empty($mobile)) {
			$mobileError = 'Please enter Mobile Number';
			$valid = false;
		}
		
		// insert data
		if ($valid) {
			$pdo = databased::connect();
			$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$sql = "INSERT INTO customers (name,email,mobile) values(?, ?, ?)";
			$q = $pdo->prepare($sql);
			$q->execute(array($name,$email,$mobile));
			databased::disconnect();
			header("Location: ../about.php");
		}
	}
?>

Wow I did a google on the error code and found on https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php a great little article
which stated

How can you find out where the premature output occured?

in header
Typical causes:

1.Print, echo
2.Raw HTML areas
3. Whitespace before <?php for "script.php line 1 " warnings
4. …
well they gave a few others
but my error was at the start of my php file there were 2 lines of space before <?php
I removed the space
and hey presto the button worked
so somehow local host can ignore this error
but an online server cant process this
Yay Soooooooooo happy
Thanks
forgot about that error listing
Thank you
THANKYOU
YAyay!!!

1 Like

The issue is the output on line 1 of create.php. If there’s nothing in the file before the <?php tag, then this likely means that the file has been saved with Byte Order Mark (BOM) characters. You need to open the file with your programming editor, pick a character encoding setting without the BOM, save the file, then re-upload it to the web hosting.

Edit: I see you posted a reply while I was typing this. The reason this ‘worked’ on your development system is because php’s output_buffering setting is turned on/set to a specific value. You should set this to OFF on your development system. so that your code will work regardless of this setting’s value.

1 Like

wow thanks for tis Mabismad
I write all my code from scratch in notepad ++
saving as php
do you think the BOM character issue is a problem for me
or only with people who write code using an IDE?
The page works perfectly now
So happy
This BOM thing is fascinating
Thank you for this
Im gonna have to read up about it

Your specific issue was the blank lines before the <?php tag. The character encoding you currently have is without a BOM, since removing the blank lines allowed the header() statement to work.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.