javascript - 如何将 php 和 javascript 变量作为输入字段值发送?

标签 javascript php

我从输入字段发送多个同名值。 当我只发送 custom1 和 var 2 但我想添加一个 javascript 变量 var3 时它工作正常但是使用 document.getElemendById 分配值完全覆盖输入字段并且 custom1 和 var2 丢失。那么如何在末尾添加一个 javascript 变量 var3,以便它打印在数组索引 $pieces[2] 下;在 v.php 文件中。

 <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <?php $var2 = "custom2" ?>
    <form action="v.php" method="POST" >
    <input id="jsvar" type="text" name="custom" value="custom1,<?php echo $var2; ?>">
    <input type="submit" name="" value="send">
    </form>
    <script type="text/javascript">
            var var3 = "custom3";
            document.getElementById("jsvar").value = var3;
        </script>
    </body>
    </html>

v.php

    <?php  
    $custom = $_POST['custom'];
    $pattern = ",";
    $pieces = explode($pattern,$custom, 3);
    print_r($pieces);
    $custom1 = $pieces[0];
    $custom2 = $pieces[1];
    echo '<br>';
    echo $custom1.'<br>';
    echo $custom2.'<br>';
    echo $pieces[2];
    ?>

最佳答案

您可以附加数据而不是用新数据替换它。

var var3 = "custom3";
document.getElementById("jsvar").value += ","+var3;

关于javascript - 如何将 php 和 javascript 变量作为输入字段值发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53910327/

相关文章:

php - laravel 5.2 中的输入文件?

javascript - getElementsByClassName 和 querySelectorAll 之间的区别?

javascript - Material UI 3.9.3 未在 IE 中设置复选框样式

php - 这些尝试注入(inject)是在我的联系表格上吗?

php - 继续收到通知 : Undefined index: whatever I do

php - 从 PHP 连接 MySQL 的最有效方法?

javascript - 如何从数组中删除所有 'map' 属性

javascript - 如何让 ng-disabled 检查 ng-repeat 中的项目值(使用 AngularJS)

javascript - 从 HTML/Javascript 中的字符串变量创建大的可下载文件

php - Doctrine 的查询生成器的目的是什么?