将Jquery值传递给PHP表单

时间:2014-03-24 23:33:33

标签: php jquery

我在php页面中有这个简单的表单:

<form role="form" method="post" action="send-message.php">
    <input type="text" id="SendTo" name="SendTo" value="">
    <textarea class="form-control text-input" name="Message"></textarea>
    <button type="submit" class="btn btn-primary">Send</button>
</form>

SendTo输入的值由同一页面上的JQuery脚本设置:

$("button").click(function() {
    $("#SendTo").val($(this).siblings('.username').val());
});

然后我在send-message.php中有PHP脚本:

<?php
$message = $_POST["Message"];
$sendTo = $_POST["SendTo"];

echo $sendTo; //nothing here!

现在,为什么SendTo的值没有从表单传递到send-message.php?

这很奇怪,因为在表单中,我可以清楚地看到输入的值,但它没有传递给send-message.php。

我尝试使用GET方法并且它是一样的......

感谢您的帮助!

缺少的代码:

<?php foreach($mostCompatibleUsers as $otherUser => $score): ?>
<?php  $compatibilityScore = getCompatibilityScore($score, $maxScore); ?>
    <div class='col-sm-4 col-xs-6'>
        <div class='box-info full'>
            <div class='img-wrap'>
            <img src='images/default-profile-pic.png' alt='Image small'>
        </div>
        <div class='des-thumbnail'>
            <h4><b><?php echo $otherUser; ?></b> <small>Niveau de compatibilité: <b><?php echo $compatibilityScore; ?>%</b></small></h4>
            <div class='progress progress-striped active'>
                <div class='progress-bar progress-bar-success' role='progressbar' aria-valuenow='<?php echo $compatibilityScore; ?>' aria-valuemin='0' aria-valuemax='100' style='width: <?php echo $compatibilityScore; ?>%'></div>
            </div>
            <div class='row'>
                <div class='col-md-6'>
                    <input type="hidden" class='username' value="<?php echo $otherUser;?>"> 
                    <button type='button' class='btn btn-success btn-sm btn-block' data-toggle='modal' data-target='#myModal'><i class='fa fa-envelope'></i> Send a message</button>
                </div>
            </div>
        </div>
    </div>
</div>
<?php endforeach; ?>

更多详情 正如您在上次添加的代码中看到的那样,有一个foreach循环显示一些用户信息,一个按钮允许向该特定用户发送消息。

当我们按下按钮发送消息时,它弹出一个模态窗口,其中包含我们可以编写和发送消息的表单。它不是常规架构!我认为这不是简化情况..

4 个答案:

答案 0 :(得分:0)

您的代码中没有.username类。

通过调用$(this).siblings('.username'),您基本上只是获取表单的其他元素$(this)实际上是提交按钮。

提供更多代码会很有用。特别是.username类是。

答案 1 :(得分:0)

$("button").click(function() {
    $("#SendTo").val($(this).siblings('.username').val());
    $.post( "send-message.php", $( "form" ).serialize() );
});

答案 2 :(得分:0)

如果你确实有一个带有用户名类的输入,那么你的代码无论如何都不会起作用,因为当你尝试用javascript读取输入时,表单已经提交了。

给你的表单一个id

 <form role="form" id="form" method="post" action="send-message.php">

而是附加到其提交事件

 $("#form").submit(function(e) {
    e.preventDefault(); //prevent the form from submitting
    $("#SendTo").val($(this).siblings('.username').val()); //process the inputs
    $(this).submit(); // then submit the form
 });

答案 3 :(得分:0)

首先,在这种情况下 ,您必须在单击提交按钮时取消表单的默认提交,最简单的方法是将其类型设置为button

其次使用Jquery手动提交,如下所示:

<form id="myForm" role="form" method="post" action="send-message.php">
    <input type="text" id="SendTo" name="SendTo" value="">
    <textarea class="form-control text-input" name="Message"></textarea>
    <input type="button" id="myButton" value="send">
</form>

<script>
$("#myButton").click(function() {
    $("#SendTo").val($(this).siblings('.username').val());
    $("#myForm").submit();
});
</script>