1

I am trying to use java-script prompt to insert values into MySQL. I have a html file and a php file.

The html files saved as Test.html:

 <button onclick="myFunction()">Create Project</button>

<script>
  function myFunction() {
      var project = prompt("Please enter project name");
      if (project != null && project !="") {
          $.post("conn.php", { project : project });
      }
  }
  </script>

The conn.php:

<?php
$servername = "localhost";
$username = "root";
$password = "Password";
$dbname = "db1";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$proj = $_POST['project'];
$sql = "INSERT INTO projects (project_name) VALUES ('$proj')";


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 

The conn.php works perfectly and inserts values if I remove the post parameter.

For example, instead of this:

$proj = $_POST['project'];
$sql = "INSERT INTO projects (project_name) VALUES ('$proj')";

if i use:

$proj = "NewProject";
$sql = "INSERT INTO projects (project_name) VALUES ('$proj')";

NewProject gets inserted into the database.

I am not sure if I am missing something in my index.html which is not posting the value in the prompt to php script. I have tried echo $_POST['project']; instead of inserting into MySQL. The echo is missing.

4
  • If if(!isset($_POST['project'])), you have a problem already. $conn->query($sql) !== TRUE . Commented Jan 8, 2018 at 6:22
  • You are not getting value in $_POST['project']. Commented Jan 8, 2018 at 6:25
  • @NitinKawane Yes Commented Jan 8, 2018 at 6:26
  • @PHPglue Do I have to wrap the insert statement inside the if(!isset Commented Jan 8, 2018 at 6:27

1 Answer 1

4

I have run your given code, it runs only i have added the jquery link above script code

Please check this correction,

<button onclick="myFunction()">Create Project</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  function myFunction() {
      var project = prompt("Please enter project name");
      if (project != null && project !="") {
          $.post("conn.php", { project : project },function(response){
             console.log(response);
         });
      }
  }
  </script>

and also i have added isset() function with $_POST param in conn.php file

<?php
$servername = "localhost";
$username = "root";
$password = "Password";
$dbname = "db1";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$proj = isset($_POST['project'])?$_POST['project']:'';
$sql = "INSERT INTO projects (project_name) VALUES ('$proj')";


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 
Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks...It did work...I did had the script pointing to jquery.min.js...however, isset method did the magic i guess. Thank you again. I will accept your answer
The answer is very poor and no instruction to the OP was given regarding his mistake. Furthermore “isset” is not a “method” and the jquery should be added in the head section of the page. Overall very poor answer.

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.