javascript - 使用 POST 将动态 SPAN 值发送到 PHP 脚本

标签 javascript php html

我正在尝试使用 PHP 脚本发送数据。我用jqCron我的问题是我有动态 SPAN 值,但该跨度的值没有使用 post 函数发送。我无法获取该值

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $TiData = !empty($_POST['BCTiData']) ? $_POST["BCTiData"] : '';
    echo $TiData;
} 
?>

这是表格

<form id="fo1" action="#" method="post"> 
    <div class="timer"> </div>
    <input type="text" class="form-control" id="BCTiData" name="BCTiData" value="<span class="timer-span"></span>">
</form>

我也尝试这个方法:

$degerSpan = '<span class="timer-span"></span>'; // Not working

如何获取Span中的数据以及如何使用POST发送? 谢谢

我也把它放在这里Pastebin

最佳答案

您需要使用javascript从SPAN元素中提取值,然后将其放入隐藏的表单字段中,然后提交数据。 这可以通过多种方式来完成。以下是一些可以帮助您入门的模型代码:

<!-- the Span that will contain the data we're interested in -->
<span class="cronMDMtimer-span"></span>

<!-- The HTML Form that will submit the timer value, this is populated using javascript -->
<form onsubmit="myFunction()" id="my-form">
    <input type="hidden" name="timerValue" id="timerValue" value="not yet defined">
    <input type="submit">
</form>

<script>
// This function gets called once the user submits the form
function myFunction(){

    // First get the value from the cronMDMtimer-span
    timerValue = $('.cronMDMtimer-span').html();

    // Then store the extracted timerValue in a hidden form field
    $("#timerValue").val(timerValue);

    // submit the form using it's ID "my-form"
    $("#my-form").submit();
}
</script>

关于javascript - 使用 POST 将动态 SPAN 值发送到 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999714/

相关文章:

javascript - 如何在单击任务时为其添加下划线并将其保存到数据库

javascript - 我想计算许多输入 javascript 并提交给 mysqli

html - 如何使用内联 css 将图像移动到 div 的中心?

javascript - 如何使用 Python 和/或 Javascript 自动验证 ngrok 密码?

javascript - 为什么值 0 没有被解析,但其他数字却被解析?

php - 通过xpath获取div的HTML内容

jquery - 用于缩小导航栏的 scrollTop() if/else switchClass() 动画

html - java.sql.SQLException : [Microsoft][ODBC Driver Manager] Invalid cursor state while inserting values into database 异常

javascript - 尝试用 JSON 编写函数,在函数调用中获取 "Unexpected token"

php - Mysql数据库连接错误