0

I am having trouble showing the output that i called in printf in php tutorial. I just followed the tutorial but still can't figure out what is wrong in displaying the variables inside the printf in localhost. Is anyone can help me. Thank you.

    <?php
            $name = '';
            $password = '';
            $gender = '';
            $color = '';
            $languages = [];
            $comments = '';
            $tc = '';
        
           if (isset($_POST['submit'])) {
                if (isset($_POST['name'])) {
                    $name = $_POST['name'];
                };
                if (isset($_POST['password'])) {  
                    $password = $_POST['password'];
                };
                if (isset($_POST['gender'])) {
                    $gender = $_POST['gender'];
                };
                if (isset($_POST['color'])) {
                    $color = $_POST['color'];
                };
                if (isset($_POST['languages'])) {
                    $languages = $_POST['languages'];
                };
                if (isset($_POST['comments'])) {
                    $comments = $_POST['comments'];
                };
                if (isset($_POST['tc'])) {
                    $tc = $_POST['tc'];
                };
            //here's the problem i cant resolve printing out the output//
            printf('User name: %s
                <br>Password: %s
                <br>Gender: %s
                <br>Color: %s
                <br>Language(s): %s
                <br>Comments: %s
                <br>T&amp;C: %s',
                htmlspecialchars($name, ENT_QUOTES),
                htmlspecialchars($password, ENT_QUOTES),
                htmlspecialchars($gender, ENT_QUOTES),
                htmlspecialchars($color, ENT_QUOTES),
                htmlspecialchars(implode('', $languages), ENT_QUOTES),
                htmlspecialchars($comments, ENT_QUOTES),
                htmlspecialchars($tc, ENT_QUOTES));
        
                
            }
        ?>
        
        
        <form action="" 
            method="post">
              User name:  <input type="text" name="name"><br>
              Password:  <input type="password" value="password"><br>
              Gender:
              <input type="radio" name="gender" value="f"> female
              <input type="radio" name="gender" value="m"> male
              <input type="radio" name="gender" value="o"> other<br/>
              Favorite color:
              <select name="color">
                <option value="">Please select</option>
                <option value="#f00">red</option>
                <option value="#0f0">green</option>
                <option value="#00f">blue</option>
        </select><br>
            Languages spoken:
            <select name="languages[]"multiple size="3">
                <option value="en">English</option>
                <option value="fr">French</option>
                <option value="it">Italian</option>
                </select><br>
                Comments: <textarea name="comments"></textarea><br>
            <input type="checkbox" name="tc" value="ok"> I accept the T&amp;C<br> 
        <input type="submit" value="Register">
        
        </form>`
2
  • 2
    What is the problem, exactly? Are you getting an error message? No output? The wrong output? Commented Jul 5, 2022 at 10:13
  • 5
    if (isset($_POST['submit'])) - will never be true, you do not have any form field with that name. Commented Jul 5, 2022 at 10:13

2 Answers 2

1

The problem here is if (isset($_POST['submit'])) there is no any form field with the name submit and it will never become true to execute. Remove the if Condition with submit or Else give the sumbit button name as Submit

<input type="submit" value="Register" name="submit">
Sign up to request clarification or add additional context in comments.

Comments

0

Errors in your previous code are:

  1. No semicolons ; are needed after ending if statements.
  2. No name attribute is specified for password input in the form.
  3. Posting form data as if (isset($_POST['submit'])) {...} without specifying the name="submit" attribute in the form.
  4. Invalid use of implode function which will generate output be like enfrit or vice versa.

Additional Tips

  1. Add the required attribute to each input to prevent from sending empty form data.
  2. You have already specified method="post" in the form tag so, no need for validating each input field as if (isset($_POST['fieldname'])) {...} . if (isset($_POST['submit'])) {...} will send all the form data as POST method.

And lastly, here is your updated code.

<?php

if (isset($_POST['submit'])) {
    $name = '';
    $password = '';
    $gender = '';
    $color = '';
    $languages = [];
    $comments = '';
    $tc = '';

    if (isset($_POST['name'])) {
        $name = $_POST['name'];
    }
    if (isset($_POST['password'])) {
        $password = $_POST['password'];
    }
    if (isset($_POST['gender'])) {
        $gender = $_POST['gender'];
    }
    if (isset($_POST['color'])) {
        $color = $_POST['color'];
    }
    if (isset($_POST['languages'])) {
        $languages = $_POST['languages'];
    }
    if (isset($_POST['comments'])) {
        $comments = $_POST['comments'];
    }
    if (isset($_POST['tc'])) {
        $tc = $_POST['tc'];
    }

    //print output
    printf('User name: %s
    <br>Password: %s
    <br>Gender: %s
    <br>Color: %s
    <br>Language(s): %s
    <br>Comments: %s
    <br>T &amp; C: %s
    <br><br>',
    htmlspecialchars($name, ENT_QUOTES),
    htmlspecialchars($password, ENT_QUOTES),
    htmlspecialchars($gender, ENT_QUOTES),
    htmlspecialchars($color, ENT_QUOTES),
    htmlspecialchars(implode(', ', $languages), ENT_QUOTES),
    htmlspecialchars($comments, ENT_QUOTES),
    htmlspecialchars($tc, ENT_QUOTES));
}

?>

<form action="" method="post">
    User name: <input type="text" name="name">
    <br>
    Password: <input type="password" name="password">
    <br>
    Gender:
    <input type="radio" name="gender" value="f"> female
    <input type="radio" name="gender" value="m"> male
    <input type="radio" name="gender" value="o"> other
    <br>
    Favorite color:
    <select name="color">
        <option value="">Please select</option>
        <option value="#f00">red</option>
        <option value="#0f0">green</option>
        <option value="#00f">blue</option>
    </select>
    <br>
    Languages spoken:
    <select name="languages[]" multiple size="3">
        <option value="en">English</option>
        <option value="fr">French</option>
        <option value="it">Italian</option>
    </select>
    <br>
    Comments: <textarea name="comments"></textarea>
    <br>
    <input type="checkbox" name="tc" value="ok"> I accept the T &amp; C
    <br>
    <input type="submit" name="submit" value="Register">
</form>

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.