Beep
Beep

Reputation: 2823

pass form values into my php data array

I have a simple form for username and password, how do I pass the values input via the user into my php array?

My Array

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

Form With Array

<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

Upvotes: 0

Views: 107

Answers (1)

Auris
Auris

Reputation: 1329

Input name gets passed as a key in to the post or get array:

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

If you set your form to post (this is recommended for logins):

<form method = 'POST'>

You can access them via $_POST superglobal:

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

So in your case:

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

Upvotes: 2

Related Questions