jsalves
jsalves

Reputation: 17

PHP curl to POST form data to REST API

I'm new to PHP, and I'm developing a simple client for one of my subjects in college. The main goal of this client, is to do CRUD into a JAVA API. After a little research, I saw that for simple clients like this one, people use CURL. I have never worked with curl and I don't know if I'm doing something wrong. When I submit my form, I get this errors.

"Notice: Undefined index: name"

"Notice: Undefined index: description"

What can I do to fix this? If anyone could help me, I would be grateful!

HTML FORM

<form class="form" action="createActivity.php">
    <label for="name" class="labelActivityName"><b>Name</b></label>
    <input type="text" id="name" placeholder="Name" name="name">

    <label for="comment" class="labelActivityDescription"><b>Description</b></label>
    <textarea id="description" placeholder="Description..." name="description"></textarea>

    <button type="submit"><b>Submit</b></button>
</form>

PHP

$url = "http://localhost:8080/tourism/api/activities";

$username = 'user';
$password = 'user123';

$fields = array(
    'name' => $_POST['name'],
    'description' => $_POST['description']
);

$client = curl_init();
curl_setopt($client, CURLOPT_URL, $url);
curl_setopt($client, CURLOPT_RETURNTRANSFER,1);
curl_setopt($client, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($client, CURLOPT_USERPWD, "$username:$password");
curl_setopt($client, CURLOPT_POST, count($fields));
curl_setopt($client, CURLOPT_POSTFIELDS, $fields);

$response = curl_exec($client);
curl_close($client);

Upvotes: 0

Views: 2954

Answers (2)

Hitesh Kumar
Hitesh Kumar

Reputation: 373

You should add method='post' in your form tag and add name attribute to button tag

<form class="form" method="post" action="createActivity.php">
            <label for="name" class="labelActivityName"><b>Name</b></label>
            <input type="text" id="name" placeholder="Name" name="name">

            <label for="comment" class="labelActivityDescription"><b>Description</b></label>
            <textarea id="description" placeholder="Description..." name="description"></textarea>

            <button type="submit" name="submit"><b>Submit</b></button>
        </form>

Your php code should be like this -

       if(isset($_POST['submit']))
        {
        $url = "http://localhost:8080/tourism/api/activities";

        $username = 'user';
        $password = 'user123';

        $fields = array(
            'name'=>($_POST['name']),
            'description'=>($_POST['description'])
        );

        $client = curl_init();
        curl_setopt($client, CURLOPT_URL, $url);
        curl_setopt($client, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($client, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($client, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($client, CURLOPT_POST, count($fields));
        curl_setopt($client, CURLOPT_POSTFIELDS, $fields);

        $response = curl_exec($client);
        curl_close($client);
      }

Upvotes: 0

Forge Web Design
Forge Web Design

Reputation: 835

You should use form method POST because by default method of from is GET and you have collect data by $_POST try following code :

<form class="form" method="post" action="createActivity.php">
    <label for="name" class="labelActivityName"><b>Name</b></label>
    <input type="text" id="name" placeholder="Name" name="name">

    <label for="comment" class="labelActivityDescription"><b>Description</b></label>
    <textarea id="description" placeholder="Description..." name="description"></textarea>

    <button type="submit"><b>Submit</b></button>
</form>

Upvotes: 4

Related Questions