0

I am trying to post some data to the server, which is running MySQL. I am running the following code but the console doesn't show me any error. Could you please take a look and tell me if you see something wrong? Any help would be appreciate because I can't find similar guidelines around..

What I want my code to do is this: I want the user, through the window.prompt, to give 4 values which will be stored in the variables: user_id, book_id, game_id, site_id. Then these 4 values must be stored in my database...

index.html

    <html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script>
    function save3()
    {
    $("#user_id").val(prompt("Give the UserId:"))
    $("#book_id").val(prompt("Give the BookId:"))
    $("#game_id").val(prompt("Give the GameId:"))
    $("#site_id").val(prompt("Give the SiteId:"))
    }
   </script>

</head>
  <body>

<p align="center">example</p>
<table align="center" width="730">
<tr>
<td align="center">
<div>
  <table class="blueTable" style="float: left">
    <thead>
<tr>
 <th colspan="1"><u>Menu</u></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="button" value="New" id="new" onclick="new1()" class="button12"/></td></tr>
<tr>
<td><input type="button" value="Load" id="load" onclick="load2()" class="button12"/></td></tr>
<tr>
<td><form name="SaveGame" id="SaveGame" method="post" action="http://127.0.0.1/PHP/mine2.php" enctype="multipart/form-data">
<input type="submit" value="Save" id="save" onclick="save3()" class="button12"/>
<input type="hidden" name="user_id" id="user_id" >
<input type="hidden" name="book_id" id="book_id" >
<input type="hidden" name="game_id" id="game_id" >
<input type="hidden" name="site_id" id="site_id" >
</form>
 <script>
        $("#SaveGame").submit(function(e) {


    var form = $(this);
    var url = form.attr('action');

        $.ajax({
               type: "POST",
               url: url,
               data: form.serialize(), // serializes the form's elements.
               success: function(data)
               {
                   alert("The game has been saved!"); // show response from the php script.
               }
             });

        e.preventDefault(); // avoid to execute the actual submit of the form.
    });
    </script>
</body>
</html>

mine2.php

    <?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];

if (mysql_query("INSERT INTO `components` (`user_id`, `book_id`, `game_id`, `site_id`) VALUES ('".$user_id."','".$book_id."','".$game_id."', '".$site_id."',)"))

// Attempt insert query execution
//$sql = "INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('6', '6', '6', '6')";
//if(mysqli_query($link, $sql)){
//} else{
//}

// Close connection
mysqli_close($link);
?>

Guys, also I found this question: JS Prompt to PHP Variable Should I use something from there?

Stelios
  • 71
  • 7
  • 2
    `mysqli_connect` ... `mysql_query` ? You'll be wanting `mysqli_query` there unless that's just a typo on SO. – CD001 Aug 30 '18 at 11:42
  • 1
    You are not able to get the data of javascript variable inside of form.serialize(). You have to create a hidden variable and set the value inside that. – Bhavin Solanki Aug 30 '18 at 11:42
  • @BhavinSolanki could you please provide me with a little help for this? Because i saw that many people use this technique.. – Stelios Aug 30 '18 at 11:46
  • 1
    var data = form.serializeArray(); data.push({user_id: user_id, book_id: book_id}); – Bhavin Solanki Aug 30 '18 at 11:49
  • OK @BhavinSolanki. So i have to insert to javascript these 2 lines of codes.Right? But what i have to write at server's side? – Stelios Aug 30 '18 at 11:53
  • 1
    You can get the same data in $_POST – Bhavin Solanki Aug 30 '18 at 11:53
  • @BhavinSolanki i think that i make something wrong. I inserted these 2 lines in javascript but nothing happens.. var data = form.serializeArray(); data.push({user_id: user_id, book_id: book_id, game_id: game_id, site_id: site_id}); am i missing something? – Stelios Aug 30 '18 at 11:57
  • 1
    Your HTML is invalid, you didn't close many tags – Cid Aug 30 '18 at 12:01
  • @Cid thank you for your help. I accepted your edit. – Stelios Aug 30 '18 at 12:02

1 Answers1

1

You need to add the information you are getting from promtps into the form. The best way is using type hidden inputs:

<input type="hidden" name="user_id" id="user_id" >

and into your save3() function

$("#user_id").val(prompt("Give the UserId:"))

Same way for the other variables.

Also, you need to verify your PHP code. I saw few errors. Try using this:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = new mysqli("127.0.0.1", "root", "", "mysql3");

// Check connection
if($link->connect_errno) {
   die("ERROR: Could not connect. " . $link->connect_error);
}

$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];

if ($result = $link->query("INSERT INTO `components` (`user_id`, `book_id`, `game_id`, `site_id`) VALUES ('$user_id','$book_id','$game_id', '$site_id')") == true)
  echo json_encode($result);
else {
  echo "Error";
}

// Attempt insert query execution
//$sql = "INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('6', '6', '6', '6')";
//if(mysqli_query($link, $sql)){
//} else{
//}

// Close connection
$link->close();
?>

The method mysqli_connect and mysql_query were deprecated, I propose to use new mysqli() instead.

And check you INSERT statement, you have a syntax error.

Let me know if it works for you please.

  • thank you for your help. Yes, yesterday i found that i have to use a hidden input. I tried it but it doesn't work. Look the code if you want again, because i changed it with the changes that you advised me. Any help will be appreciate.. – Stelios Aug 31 '18 at 06:27
  • it's perfect! Thank you very much for your help again. – Stelios Aug 31 '18 at 10:25
  • if i want to make a select query using the 4 values from window.prompt and return the values of site_id, is this enough? $query = "SELECT site_id FROM components WHERE (user_id='$user_id' && book_id='$book_id' && game_id='$game_id')"; – Stelios Aug 31 '18 at 10:28
  • Nope. In this case you don't need to use parenthesis in the WHERE clause: $query = "SELECT site_id FROM components WHERE user_id='$user_id' && book_id='$book_id' && game_id='$game_id'"; Try to research a little more about MySQL statements to make your life easier xD. – Freddy Delgado Aug 31 '18 at 10:38
  • @ Freddy Delgado :) Thank you my friend! I'll search for it. – Stelios Aug 31 '18 at 10:42