2017-09-26 90 views
0

我有一個簡單的形式是這樣的:HTML表單PHP函數

<form> 
    <label>Subject</label> 
    <input id="subjec_input" name="subject_input" type="text"> 
    <label>Message Body</label>    
    <textarea id="body_input" name="body_input"></textarea> 
    <button id="submit_btn" name="submit_btn">Submit</button> 
</form> 

我想借此從表單的輸入,並把subject_inputabody_inputb

<?php send_msg(2, 'a', 'b');?> 

在那一刻我有這個代碼在頁面加載時觸發該功能,但我希望它只在用戶點擊提交時觸發。

我一直在使用get嘗試,但都沒有成功

+5

向我們展示你的不成功的嘗試。 [這是非常基本的PHP。](http://php.net/manual/en/tutorial.forms.php) – Script47

+3

我們總是很樂意幫助和支持新的編碼器,但是您需要首先幫助自己。 : - )***在[**做更多研究**之後](https://meta.stackoverflow.com/q/261592/1011527)如果你有問題**發佈你已經嘗試** **清楚說明什麼不工作**並提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。閱讀[如何問](http://stackoverflow.com/help/how-to-ask)一個很好的問題。請務必[參觀](http://stackoverflow.com/tour)並閱讀[this](https://meta.stackoverflow.com/q/347937/1011527)。 –

+0

使用'$ _GET ['subject_input']'代替'a'和'$ _GET ['body_input']'代替'b' – mega6382

回答

2

如果您使用GET請求,那麼你可以,如果你使用的是POST請求,然後你的HTML表單標籤必須定義method="POST"

<?php 
    $body_input=isset($_POST['body_input'])?$_POST['body_input']:''; 
    $subject_input=isset($_POST['subject_input'])?$_POST['subject_input']:''; 

    send_msg(2,$body_input, $subject_input); 
+0

謝謝。我使用'post'是因爲'get'正在改變URL。然後我使用這個:https://stackoverflow.com/a/26380651/1045794來隱藏帖子。 :) – redditor

0

您需要使用:

if(isset($_POST['submit_btn'])){ 

$a = $_POST['subjec_input']; 
$b = $_POST['body_input']; 

//Run your function/code. 

} 

改變你的形式方法 「後」:

<form action="" method="post"> 

此外,編輯按鈕爲「提交」類型:

<button type="submit" id="submit_btn" name="submit_btn">Submit</button> 
0
if(isset($_POST['submit_btn'])){ 
    echo $subject_input = $_POST["subject_input"]; 
    echo $body_input = $_POST["body_input"]; 
send_msg(2,$body_input, $subject_input); 

} 

您可以使用它像這樣做

<?php 
$body_input=$_GET['body_input']; 
$subject_input=$_GET['subject_input']; 

send_msg(2,$body_input, $subject_input); 

+3

這不適用於OP的形式。 – Script47

0

首先給你的表格一個類似GET/POST的方法

<form action="welcome.php" method="get"> 

然後在你的PHP代碼中他們獲得與超級全局 像:

$a = $_GET['body_input'] 

然後這些信息傳遞給你的函數