0

i'm using Xampp and when I run the php code its self it works fine, but when I connect html and php and then I click on submit button in html, the complete php code comes out.

Any idea, what I'm missing out?

THE HTML CODE

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>

<form action="register.php" method="post">

<div>
    <label for="firstname">Firstname</label>
    <input type="text" name="firstname">
</div>

<div>
    <label for="lastname">lastname</label>
    <input type="text" name="lastname">
</div>

<button type="submit">submit</button>

</form>

</body>

 </html

THE PHP CODE

<?php

$firstname = $_POST['firstname'];
 $lastname = $_POST['lastname'];

 $conn = new mysqli('localhost', 'root', '', 'testt');

 if($conn->connec_error) {
 die ('connection failed : '.$conn->connect_error);
 } else{
    $stmt = $conn->prepare("insert into testtt(firstname, lastname) values(?,?)");
 $stmt->bind_param("ss", $firstname, $lastname);
 $stmt->execute();
 echo "Registration succesfully...";
 $stmt->close();
 $conn->close();
  }

  ?>
  • Could definitley be a configuration issue. Can you tell us what you mean by "running the php code its self works fine"? – Sherif Aug 02 '21 at 17:34
  • Does this answer your question? [PHP code is not being executed, but the code shows in the browser source code](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-but-the-code-shows-in-the-browser-source-code) – NcXNaV Aug 02 '21 at 17:52

2 Answers2

0

Many times when this happens, it is more likely that you are accessing the PHP file from the local path, instead of accessing it from your localhost server.

These are the few things to check:

  • Is your project from inside the htdocs (www in WAMP) folder? If you are using Windows and your Xampp is installed under the C:\ drive your htdocs might be found in C:\xampp\htdocs\.
  • Did you save your file with the .php extension? E.g: register.php.
  • Make sure your Xampp installation contains PHP and is not corrupted.
Erisan Olasheni
  • 2,395
  • 17
  • 20
-1

Your xampp may be missconfigured. What is your file extension .html or .php It could be that your xampp only knows .php for PHP.

Marco
  • 3,470
  • 4
  • 23
  • 35
  • I have two files the html file is .html and the php file is .php, and i run this code in different laptops, all the same! – Abdirahman Faqooshe Aug 02 '21 at 17:14
  • Can you post the httpd.conf files(s) as well? Maybe an hint can be found there. If everything's configured fine it would work... – Marco Aug 02 '21 at 17:19
  • accessing the PHP file as a file on c drive (eg. C:\xamp\htdocs\data.php or file:///c:/xamp/htdocs/data.php) instead of as a webpage (ie //localhost/data.php). – Nadeem Taj Aug 02 '21 at 18:33