" > -6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:55:18 26 4
gpt4 key购买 nike

我想将变量从我的 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 );
});
};

当我用警报测试变量是否传递时,我得到

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

这可能对您有用。

谢谢!

关于javascript - 将变量从 PHP FORM 传递到 JAVASCRIPT 并使用 JQUERY 更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19378332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com