2016-06-01 29 views
0

所以我有一個正規形式表檢查信息並插入同一頁上

<form action="includes/send.php" method="post" onsubmit="return isValidForm()" />> 

<h1>Opgeven workshops</h1> 

<label for="name">Voornaam:</label> 
<input type="text" autocomplete="off" id="name" name="firstname"> 
<label class="choice" data-id="1"><input type="checkbox" name="group1" value="use your apple1">use your apple<span class="left" ></span> 
</label>---more stuff more stuff more stuff-- 

現在我提交表單我想告訴用戶填寫表單這樣

$f_name = $_POST['firstname']; 
$l_name = $_POST['lastname']; 

U hebt zich ingeschreven bij: <br /> 
Eerste workshop : <?php echo $first; ?><br /> 
Tweede workshop : <?php echo $second; ?><br /> 
Klopt dit? 

<button type="submit" onclick="send()">Ja</button> 
<button type="submit" onclick="noSend()">nee</button> 
信息

當用戶點擊send時,它會將以前表單中的信息發送到查詢以將其插入到數據庫中。我試圖做到這一點,而不必再創建另一個「隱藏表單」,因爲當您按下按鈕時,只需讓腳本「等待」並繼續執行腳本/插入功能,它就是不必要的代碼。

我試着設置一個變量$submit= false;send功能(這是在JavaScript)內設置submittrue但似乎並沒有工作,因爲它會自動將變量設置爲true沒有按下按鈕。

function send(){ 
<?php $submit = true ?> 
var submit = <?php echo $submit ?>; 
console.log(submit); 
} 
if($submit){ 
    echo 'submitted'; 
} else { 
    echo 'not true'; 
} 
+0

JavaScript是客戶端,PHP是服務器端語言。你不能用你想要的方式在JavaScript中設置PHP變量。 – ksno

+0

你別無選擇,只能將值存儲在隱藏的輸入或會話變量中。你不能讓腳本像你說的'等待'... – Naruto

+0

我知道@ksno這就是我說的。它不起作用。這就是爲什麼我要求建議 – Devian

回答

0

在你的PHP端調用你的Javascript「send()」當值傳遞函數

<?php 
    $first = "First course"; 
    $second = "Second course"; 
?> 


U hebt zich ingeschreven bij: <br /> 
Eerste workshop : <?php echo $first; ?><br /> 
Tweede workshop : <?php echo $second; ?><br /> 
Klopt dit? 

<!-- pass the required values into the function, this is just a basic implementation you could also use a loop to fill in the values --> 

<button type="button" onclick="send(true, '<?php echo $first ?>', '<?php echo $second ?>')"> 
    Ja 
</button> 
<button type="button" onclick="send(false)"> 
    nee 
</button> 

對於接收功能,你可以實現這樣的事情

<script type="text/javascript"> 
    function send(submit){  
     //Get all arguments passed except the first variable, the submit boolean 
     var listOfVariablesToPost = Array.prototype.slice.call(arguments,1); 
     if(submit){ 
      console.log("post"); 
      console.log(listOfVariablesToPost); 
      /* Do POST here either by using XMLHttpRequest or jQuery AJAX/POST (Or any other way you like)*/ 
      /* XMLHttpRequest: http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest */ 
      /* jQuery POST https://api.jquery.com/jquery.post/ */ 
     }else{ 
      console.log("No post") 
      /* Don't post and do whatever you need to do otherwise */ 
     } 
    } 
</script> 

這是一個非常簡單的實現,但我希望它有幫助。

相關問題