0

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);

2 Answers 2

4

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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);
      }

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.