将Jquery值传递给PHP表单
我在php页面中有一个简单的表单:将Jquery值传递给PHP表单,php,jquery,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 ty
<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>
然后我在send-message.PHP中有了PHP脚本:
<?php
$message = $_POST["Message"];
$sendTo = $_POST["SendTo"];
echo $sendTo; //nothing here!
'aria valuemin='0'aria valuemax='100'style='width:%'>
更多详细信息
正如您在最后添加的代码中看到的,有一个foreach循环显示一些用户信息,还有一个按钮允许向特定用户发送消息
当我们按下按钮发送消息时,它会弹出一个模式窗口,其中包含一个表单,我们可以在其中编写和发送消息。这不是一个常规的架构!我认为这并没有简化情况。代码中没有
.username
类
通过调用$(this).sibbines('.username')
基本上就是获取表单的其他元素,因为$(this)
实际上是提交按钮
提供更多的代码会很有用。特别是在
.username
类为的情况下。如果您确实有一个带有username类的输入,那么您的代码无论如何都不会工作,因为在您尝试使用javascript读取输入时表单已经提交
$("button").click(function() {
$("#SendTo").val($(this).siblings('.username').val());
$.post( "send-message.php", $( "form" ).serialize() );
});
给你的表格一个id
<form role="form" id="form" method="post" action="send-message.php">
首先,在这种情况下,您必须在单击submit按钮时取消表单的默认提交,最简单的方法是将其类型设置为按钮
其次,使用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>
$(“#我的按钮”)。单击(函数(){
$(“#SendTo”).val($(this).sides('.username').val());
$(“#我的表格”).submit();
});
您能提供完整的html吗?用户名类在哪里?哪个按钮用于输入设置?可能是因为没有类为.username
的元素。当我运行页面时,我可以看到SendTo input的文本。所以我想在那一点上一切都很好。问题是该值没有从表单传递到send-message.php!我在那里用.username
在本地试用过,效果很好。谢谢。我试过了,没什么区别。
<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
});
<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>