PHP code will not run - only the contents of the file will be displayed
P粉493534105
P粉493534105 2024-02-03 22:04:44
0
1
495

This is my first time using HTML PHP and Ajax, so please be patient. Most of the code comes from examples I found online. However, I can't get it to actually insert into the database. The ajax function does go into the success function; the success output is just displayed as an alert in the php file.

I have this all local, so I launch chrome as

chrome --allow-file-access-from-files file:///C:/filepath/index2.html

I know it's not good, but I'm making do.

index.html

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <title>Insert</title>
</head>
<body>
    <label>Name</label>
    <input type="text" id="name"> 
    <label>Email</label>
    <input type="text" id="email">
    <button type="submit" id="button">SAVE</button>    
    <script>
        $(document).ready(function(){
            $("#button").click(function(){
                var name=$("#name").val();
                var email=$("#email").val();
                $.ajax({
                    url:'insert.php',
                    method:'POST',
                    data:{
                        name:name,
                        email:email
                    },
                   success:function(data){
                       alert(data);
                   },
                   error:function(data){
                        alert(JSON.stringify(data));
                   }
                });
            });
        });
    </script>
</body>
</html>

Insert.php

<?php
    $name=$_POST['name'];
    $email=$_POST['email'];
    $conn = new mysqli('Azure server URL', 'Username','Password', 'tableName');
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $sql="INSERT INTO data ('id', 'name', 'email') VALUES (NULL, $name, $email)";
    if ($conn->query($sql) === TRUE) {
        echo "data inserted";
    }
    else 
    {
        echo "failed";
    }
?>

P粉493534105
P粉493534105

reply all(1)
P粉517475670

you say

...Not only is this bad, it's the cause of the problem. Chrome Your file system cannot execute PHP code.

You need to use an appropriate web server with a working PHP runtime so that it supports executing PHP in response to HTTP requests. Use XAMPP or Laragon to install a fully functional PHP development environment where PHP, Apache, and MySQL/MariaDB are available and configured so you can develop and test properly on your local machine.

Another option for a quick fix is ​​the PHP built-in web server This is available once you have PHP installed, although its functionality is more limited, it doesn't closely resemble a real deployment environment, obviously if you Components such as databases are also required, and you need to set them up separately.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template