将变量从 PHP FORM 传递到 JAVASCRIPT 并使用 JQUERY 更新数据库


passing variables from PHP FORM to JAVASCRIPT and update DB with JQUERY

我想将变量从我的php形式传递给javascript:

php 表单与 javascript 代码位于同一文件中:

// Version2
<?php //echo "<form method='"post'" onsubmit='"return addCommentsToDB(starttime,endtime,text,cat)();'" action=". $_SERVER['PHP_SELF']." >"; ?>
<form method="post" onsubmit="addCommentsToDB()" action="<?php $_SERVER['PHP_SELF']?>" >
        <table width="150px" border="1" cellpadding="0">
          <tr>
            <td width="25px"><p>start</p></td>
            <td width="25px"><p>end</p></td>
            <td width="75px"><p>Text</p></td>
            <td width="10px"><p>cat</p></td>
            <td width="5px"><p>+</p></td>
          </tr>
         <tr>
            <td><input style="width: 75px" type="time" name="starttime" id="starttime"></td>
            <td><input style="width: 75px" type="time" name="endtime" id="endtime"></td>
            <td><input style="width: 75px" type="text" name="text" id="text"><input type="hidden" id="VideoId" name="VideoId" value="<?php echo $video_id; ?>"></td>
            <td>
              <select width="15" name="cat" id="cat">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
              </select></td>
            <td><input name="ADD"  type="submit" value="add">  </td>
            </tr>
        </table>        </form>

添加注释功能:

函数 addCommentsToDB () {

var starttime = document.getElementById('starttime');
var endtime = document.getElementById('endtime');
var text = document.getElementById('text');
var cat = document.getElementById('cat');
alert ("starttime ="+starttime);
$.ajax({
  url: "addCommentsToDB.php",
  type: "POST",
  data: {starttime: starttime, endtime: endtime, text: text, cat: cat},
        });
 request.done(function( msg ) {
      $( "#log" ).html( msg );
      $("#div2").load('loadTable.php', 
        {    'videoId': '<?php// echo $video_id ?>'
           } );
      $("#div3").load('multiTimeline.php', 
        {    'videoId': '<?php// echo $video_id ?>'
           } );
});
        request.fail(function( jqXHR, textStatus ) { 
        alert( "Request failed: " + textStatus );
        }); 
};

当我通过警报测试变量时,我得到

警报:Starttime =[object HTMLInputElement]

我还尝试了该代码来接收变量(带有表单标题版本 2):

function addCommentsToDB (starttime, endtime, text, cat) {
    $.ajax({
      url: "addCommentsToDB.php",
      type: "POST",
      data: {starttime: starttime, endtime: endtime, text: text, cat: cat},
            });
     request.done(function( msg ) {
          $( "#log" ).html( msg );
          $("#div2").load('loadTable.php', 
            {    'videoId': '<?php// echo $video_id ?>'
               } );
          $("#div3").load('multiTimeline.php', 
            {    'videoId': '<?php// echo $video_id ?>'
               } );
    });
            request.fail(function( jqXHR, textStatus ) { 
            alert( "Request failed: " + textStatus );
            }); 
    };

使用以下代码:

var starttime = document.getElementById('starttime').value;
var endtime = document.getElementById('endtime').value;
var text = document.getElementById('text').value;
var cat = document.getElementById('cat').value;

var starttime = $('#starttime').val();
var endtime = $('#endtime').val();
var text = $('#text').val();
var cat = $('#cat').va();

这可能对您有用。

谢谢!

您正在尝试将 DOM 元素传递给 PHP 脚本。当它被转换为 JSON 字符串时,一个对象被写入为 [object HTMLInputElement] .相反,您应该传递元素的值。试试这个:

$.ajax({
    url: "addCommentsToDB.php",
    type: "POST",
    data: {
        starttime: $('#starttime').val(), 
        endtime: $('#endtime').val(), 
        text: $('#text').val(), 
        cat: $('#cat').val()
    }
})

请注意,为了简单起见,我将getElementById用法转换为jQuery选择器。

你可以断言一个 ID/class 来形成和序列化() 它