I want to send data from window.prompt to my server, but maybe something is missing (javascript+php)

问题: I am trying to post 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...

问题:

I am trying to post 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 appreciated 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."',)"));

mysqli_query($link ,""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?


回答1:

<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://localhost/learnlaravel/oops-example/mine-2.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) {
                                                        console.log(data);
                                                        return false;
                                                        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>

<?php
if(!empty($_POST)) {

    /* Attempt MySQL server connection. Assuming you are running MySQL
    server with default setting (user 'root' with no password) */
    $link = mysqli_connect("localhost", "root", "", "homestead");
    // 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'];

    mysqli_query($link ,"INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('".$user_id."','".$book_id."','".$game_id."', '".$site_id."')") ;

    // Close connection
    mysqli_close($link);
    echo 'Y'; exit;
}

this is work for me you have mistake in query just remove "," last end of '".$site_id.".


回答2:

you have error in sql query please remove comma after $site_id i tried with the below code it worked

use below code:-

 $qry ="INSERT INTO `components` (`user_id`, `book_id`, `game_id`, `site_id`) VALUES ('".$user_id."','".$book_id."','".$game_id."', '".$site_id."')";
        if($qry){
        mysqli_query($link,$qry);
        }
  • 发表于 2018-09-01 08:50
  • 阅读 ( 293 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除