繁体   English   中英

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

[英]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>

addcomments功能:

function 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 /类来形成和序列化()它

暂无
暂无

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

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