2017-04-19 65 views
0

我有一個簡單的用戶名和密碼錶單,我如何將通過用戶輸入的值傳遞到我的php數組中?將表單值傳遞到我的PHP數據數組中

我的數組

$post_data = array(
     'username' => 'user', 
     'password' => 'pass#', 
    ); 

形態 - 陣列

<form action=""> 
    <div class=""> 
     <label><b>Username</b></label> 
     <input type="text" placeholder="Enter Username" name="uname" required> 

     <label><b>Password</b></label> 
     <input type="password" placeholder="Enter Password" name="psw" required> 

     <button type="submit">Login</button> 
    </div> 
</form> 
<?php 

session_start(); 

$service_url = 'http://localhost/app/sapi/user/login.xml'; // .xml asks for xml data in response 
$post_data = array(
    'username' => 'user', 
    'password' => 'pass#', 
); 
$post_data = http_build_query($post_data, '', '&'); // Format post data as application/x-www-form-urlencoded 

回答

2

輸入名稱被作爲重點傳遞到後或得到數組:

$_GET['uname'] and $_GET['psw']. 

如果你設置你的表格發佈(這個我的推薦用於登錄):

<form method = 'POST'> 

您可以通過$ _POST超全局訪問它們:

$_POST['uname'] and $_POST['psw']. 
你的情況

所以:

if(isset($_POST['uname']) && isset($_POST['psw'])) 
{ 
    $post_data = array(
     'username' => $_POST['uname'], 
     'password' => $_POST['psw'], 
    ); 
}