javascript - 如何使用ajax发送表单和id

标签 javascript php jquery ajax

我创建了一个ajax函数来在我的数据库中输入一个表单,但我还需要携带用户ID,这样我就可以知道该表属于谁。我已经用ajax尝试过:

data: $("#add_form").serialize(), { id : <?php echo $_SESSION['user_id']; ?> }, 

但这不起作用,我只是收到此错误:Uncaught SyntaxError: Unexpected token , 我如何在 Ajax 中使用表单发送我的 ID?这是我的表格:

<form action="" method="post" id="add_form" name="add_form">            
<h5>Name: </h5><input type="text" name="name" class="addinput"/>
<h5>Artist: </h5><input type="text" name="artist" class="addinput"/>
<h5>Link to song: </h5><input type="text" name="url" class="addinput"/>
<h5>Lyrics: </h5><textarea name="lyrics" class="addinputtext">Here goes the lyrics...</textarea>
<input type="button" value="Add track" class="loginbtn" /> 
</form>

我的完整ajax调用:

// this is the id of the form
                        $("#add_form").submit(function() {

                            var url = "includes/addtrack.php"; // the script where you handle the form input.

                            $.ajax({
                                   type: "POST",
                                   url: url,
                                   data: $("#add_form").serialize(), { id : <?php echo $_SESSION['user_id']; ?> }, // serializes the form's elements.
                                   success: function(data)
                                   {
                                       alert(data); // show response from the php script.
                                        $.ajax({ //Get the name of the artist 
                                          url: "includes/getsongs.php",
                                          type: "POST",
                                          data: {id: <?php echo $_SESSION["user_id"]; ?>},
                                          success: function(data) {
                                            //called when successful
                                            console.log("The data is:");
                                            console.log(data);
                                            $(".yoursongs").html(data); //Update
                                          },
                                          error: function(e) {
                                            //called when there is an error
                                            console.log(e.message);
                                          }
                                        });
                                   }
                                 });

                            return false; // avoid to execute the actual submit of the form.
                        });

PHP 代码

    <?php 

include 'db_connect.php';

$stmt = $mysqli->prepare("INSERT INTO song VALUES (?)");
$stmt->bind_param('ssssi', $name, $artist, $url, $lyrics, $userid);   // bind $sample to the parameter

// escape the POST data for added protection
$name = isset($_POST['name'])
          ? $mysqli->real_escape_string($_POST['name'])
          : '';
$artist = isset($_POST['artist'])
          ? $mysqli->real_escape_string($_POST['artist'])
          : '';
$url = isset($_POST['url'])
          ? $mysqli->real_escape_string($_POST['url'])
          : '';
$lyrics = isset($_POST['lyrics'])
          ? $mysqli->real_escape_string($_POST['lyrics'])
          : '';
$userid = isset($_POST['userid'])
          ? $mysqli->real_escape_string($_POST['userid'])
          : '';

/* execute prepared statement */
$stmt->execute();

printf("%d Row inserted.\n", $stmt->affected_rows);
echo 'done';

/* close statement and connection */
$stmt->close();


?>

最佳答案

目前您的data设置不正确,因为serialize()会将表单值转换为查询字符串,因此您可以使用&将您的 id 值连接到此字符串中:

data : $('#add_form').serialize() + "&id=" + <?php echo $_SESSION['user_id']; ?>,

而不是:

data: $("#add_form").serialize(), { id : <?php echo $_SESSION['user_id']; ?> },

关于javascript - 如何使用ajax发送表单和id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23130458/

相关文章:

javascript - 如何使用 Strapi 创建自定义注册和登录 API?

javascript - 单独定义服务和工厂时,Angular 不起作用

javascript - 仅在点击按钮后显示 Google 客户评论

php - 直接从 PHP 调用 Azure 存储 REST API

php 变量转为 JSON 对象

javascript - 从 Javascript 文本框中删除制表符空间

javascript - 基于对象数组中不同单位的总和值

javascript - Angular 指令调用父 Controller 函数

php - 无法将列从字符变化更改为整数,但需要查询字符串 >

jQuery 验证多个输入 :file