4

I'm trying to put a button [that will redirect to the home page] inside a .php file that comes as a result of a submit form.

The php file is this:

<?php 

$user = 'root';
$pass = '';
$db = 'testdb';

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect.");

$sql = "INSERT INTO Customers (CompanyName, City, Country) VALUES ('$_POST[post_company]', '$_POST[post_city]', '$_POST[post_country]')";

if(mysqli_query($conn, $sql)){
    echo "New record created successfully!";
    $last_id = $conn->insert_id;
    echo "\nNew record has ID: " . $last_id;
}else{
    echo "Error 1: " . $sql . "<br>" .mysqli_error($conn);
}


// echo("<button onclick='location.href='index.html'>Back to Home</button>");
// this is a test I did before and didn't work

$conn->close();

?>

<div id="center_button"><button onclick="location.href='index.html'">Back to Home</button></div>

I 've tried making this an .html file and put inside the php code, but didn't work.
I saw that you can place html code inside a php file, but this doesn't seem to work either. I've also tried it without the div tag, and also tried putting html and body tags, too.
The database works fine btw, it updates normally, I just can't seem to make the html code appear.


What am I doing wrong?

1
  • Your test had broken quoting. You changed the double quotes to single quotes to avoid coming out of the string, but then this breaks the quoting around index.html. Commented May 24, 2015 at 20:20

4 Answers 4

6

You're missing an HTML tag. That's why you cannot see your button.

?>
<html>
<head>
</head>

<body>
<div id="center_button">
    <button onclick="location.href='index.html'">Back to Home</button>
</div>
</body>
</html>

EDIT : remove this line :

header("Content-Type: application/json; charset=UTF-8");
Sign up to request clarification or add additional context in comments.

2 Comments

tried it - it might display errors but header was messing with html tags.
EDIT: Yeap that actually worked!! Guess I will not have greek letters on this page. ;p All of the answers posted worked after I removed this line. [Didn't actually need the html tag though :) ] Thank you!
3

Your test had broken quoting. You need to escape the double quotes rather than replacing them with single quotes, as otherwise your 'index.html' link won't work. As I now have a spare 5 mins...

echo("<button onclick=\"location.href='index.html'\">Back to Home</button>");

A string inside a string inside a string needs care;-)

2 Comments

You mean like echo("<button onclick='location.href='index.html''>Back to Home</button>"); or echo"<button onclick='location.href='index.html''>Back to Home</button>"; these didn't work either...
Your edit didn't work either... This is getting weird. Is there a problem maybe that it's a .php file coming from a form [ <form action="submitDB.php" method="post"> ]?
1

This is what worked for me.

<a><?php echo( "<button onclick= \"location.href='inc/name_of_php_file.php'\">your buttons name goes here</button>");?></a>

In other words nest your desired php code block inside an anchor element to 'link' it. Then add the php code block and inside that code block you'll want to add an echo(). Inside that echo you want to add your HTML code like normal. As if you weren't even writing in PHP.

Edit the folder name and file to your desired location. And then finally edit what text you want your button to show your viewers.

onclick= \"location.href='folder_name_goes_here/name_of_php_file_goes_here.php'\">your buttons name goes here

This should link to your page with your desired caption.

Please keep in mind the \" as this is utterly important. Without \ in front of the " your code wont read correctly due to the many "s found nested inside each other. \ allows HTML to ignore the first " as an end to the echo Sting.

Comments

0
echo "<button onclick=\"location.href='home.html'\">Home</button>";

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.