繁体   English   中英

尝试使用ajax和jquery序列化表单

[英]Trying to serialize form with ajax and jquery

我正在尝试获取此表单以通过ajax从jquery中的模态提交用户输入,但是ajax调用未获取用户输入。 正在对服务器进行ajax调用,但是当我检查用户输入是否已由服务器注册时,它为空白。

HTML:

<body>
  <div id="home">
    <p>Home</p>
  </div>
  <div id="welcome">
    <span>Welcome  <?php echo $_SESSION['user']; ?></span>
  </div>
  <form id="form">
    <p1>Please enter in your hotkey below</p1>
    <input type="text" id="hotkey" placeholder="Hotkey">
    <input id="submit" type="button" value="Submit">
  </form>
  <div id="keyshortcut">
    <input type="text" id="keyshortcut" placeholder="hotkey">
  </div>
  <div id="select_button">
    <button id="select-hotkey">Select Your Hotkey</button>
  </div>
  <div id="hotkeyselection">
    <span>This is the hotkey you have selected:</span>
  </div>
  <div id="logout">
    <a href="logout.php?logout">Log-Out</a>
  </div>
  <br>
</body>

jQuery:

$(function() {
   dialog = $("#form").dialog({
     autoOpen: false,
     height: 250,
     width: 350,
     modal: true,
     buttons: {
       Cancel: function() {
         dialog.dialog("close");
       }
     },
     close: function() {
       form[0].reset();
     }
});

$("#select-hotkey").button().on("click", function() {
  dialog.dialog("open");
});

AJAX:

$(document).ready(function() {
  $("#submit").click(function() {
    var hotkey = $("#hotkey").val();
    var dataString = 'hotkey=' + hotkey;
    alert(dataString);
    $.ajax({
      type: "POST",
      url: "submithotkey.php",
      data: $("#form").serialize(),
      success: function(result) {
        alert(result);
      }
    });
  });
});

的PHP

<?php
$con='';
$con= mysqli_connect("localhost","***","****","***");
$curr_user=$_SESSION['user'];
$data=$_POST['serialize'];
$hotkey=$data['hotkey'];
$query = "insert into users(Hotkey) values ('$hotkey',) where username='$curr_user'";
$run_query = mysqli_query($con, $query);
?>

您需要给表单字段一个name=

$().serialize();

将通过name创建一串参数

示例: http//jsbin.com/sowutafuju/edit?html,js,console,output

尝试在ajax中使用此数据结构

data:{hotkey:hotkey},

在PHP中,您使用以下方式获取数据:

$hotkey=$_POST['hotkey'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM