2014-03-24 50 views
1

我有一個簡單的形式在一個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()); 
}); 

然後我在發送-message.php PHP腳本:

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

echo $sendTo; //nothing here! 

現在,爲什麼的SendTo的值不是從形式傳遞給發送-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循環顯示一些用戶的信息和一個按鈕允許發送消息到那個特定的用戶。

當我們按下一個按鈕發送消息時,它會彈出一個模式窗口,其中包含一個可以編寫和發送消息的窗體。這不是一個普通的架構!我認爲這不是簡化情況..

+0

你能提供完整的HTML?用戶名級別在哪裏?哪個按鈕用於輸入設置? – sinisake

+1

可能是因爲類中沒有元素'.username' – adeneo

+0

當我運行該頁面時,可以看到SendTo輸入的文本。所以我想在這一點上一切都很好。問題是該值不會從表單傳遞給send-message.php! – max

回答

0

代碼中沒有.username類。

通過調用$(this).siblings('.username')您基本上只是獲取表單的其他元素,因爲$(this)實際上是提交按鈕。

提供更多的代碼將是有用的。尤其是.username課程。

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

謝謝。我試過了,沒有任何區別。 – max

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 
}); 
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>