我想将变量从我的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函数:

函数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 );
        });
};


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

警报:
开始时间= [对象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();


这可能对您有用。

谢谢!

09-30 16:17
查看更多