0

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

1 Answer 1

2

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'],
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.