1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP coding error-Need help

Discussion in 'PHP' started by Luca tall, Jan 20, 2014.

  1. #1
    I am a php beginner. Can anyone explain me why it is not working and please correct the code if possible.

    registrationp.php
    if(empty($_POST['password']))
    {
    $error="enter your password";
    header('Location:registration.php?errors='.$error. ' ');
    }

    registration.php
    <input type="password" name="password" id="password" placeholder="password"><?php echo $_GET['errors']; ?>
     
    Luca tall, Jan 20, 2014 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    can you explain what is happening and if you see any error message what it is? Also it is tricky to guess what other code you have that could affect the code that you have posted. and please use the code feature in the texteditor when posting code. makes it much easier to look at

    registrationp.php
    if(empty($_POST['password']))
    {
    $error="enter your password";
    header('Location:registration.php?errors='.$error. ' ');
    }
    PHP:
    registration.php
    <input type="password" name="password" id="password" placeholder="password"><?php echo $_GET['errors']; ?>
    PHP:
     
    stephan2307, Jan 20, 2014 IP
    sarahk likes this.
  3. Luca tall

    Luca tall Member

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Hi stephan,By mistakenly I point out this code has error. My error is out of this part in coding.
    Another coding error:
    <?php
    $con=mysql_connect("localhost","root","","test");
    mysql_select_db("test",$con);

    if(empty($_POST['emailid']))
    {
    header("Location:registration.php?error=enter your email id");
    }
    else{
    $emailid=$_POST['emailid'];
    }
    if(empty($_POST['name']))
    {
    header("Location:registration.php?error1=enter your name");
    }
    else{
    $name=$_POST['name'];
    }
    if(empty($_POST['age']))
    {
    header("Location:registration.php?error2=enter your age");
    }
    else{
    $age=$_POST['age'];
    }
    if(empty($_POST['password']))
    {
    $error="enter the password";
    header('Location:registration.php?errors='.$error.'');
    }
    else{
    $password=$_POST['password'];
    }
    ?>

    <html>
    <body>
    <form action="registrationp.php" method="POST">
    <input type="text" name="emailid" placeholder="Emailid" id="emailid"><?php echo $_GET['error']; ?></br>
    <input type="text" name="name" id="name" placeholder="Name"><?php echo $_GET['error1']; ?></br>
    <input type="text" name="age" id="age" placeholder="Age"><?php echo $_GET['error2']; ?></br>
    <input type="password" name="password" id="password" placeholder="password"><?php echo $_GET['errors']; ?></br>
    <input type="hidden" name="formsubmitted" value="True"></br>
    <input type="submit" value="Register">
    </form>
    </body>
    </html>
    I could like to show all field errors but it shows me only last field's error. I know I did a minor mistake in coding. but I couldnt correct that please guide me.
     
    Luca tall, Jan 20, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    The whole code is... kinda horrible. First of all, why do you use $_GET to show errors? Just use $_POST, and show errors based on which input contains no or wrong info.
    Something like this:

    First, change your submit to also have a name-attribute, something like:
    
    <input type="submit" name="submit_form" value="Register">
    
    PHP:
    Then:
    
    $errors = array();
    if (isset($_POST['submit_form'])) {
        if (empty($_POST['emailid'])) {
          $errors['emailid'] = 'Please enter your emailid';
        }
        if (empty($_POST['name'])) {
          $errors['name'] = 'Enter your name';
        }
    }
    
    PHP:
    Then you use the array to show errors, based on whether or not the array_key_exists - something like this:
    
    <input type="text" name="emailid"><?php if (array_key_exists('emailid',$errors)) { echo $errors['emailid']; } ?>
    
    PHP:
    And so on. And for the love of god, learn how to use the [ code ]-feature
     
    PoPSiCLe, Jan 20, 2014 IP