Jump to content

form trouble


fantomel

Recommended Posts

hello i'm having some troubles with a form can someone help me ? i want to output some errors in a certain place. but i can't manage do it:(

 

<?php 
function show_register()
{
$error_string = '';
echo '<form id="loginform" method="post" action="">
    <fieldset>
        <legend>
            Register
        </legend>
        <p>
             '. $error_string.'
        </p>
        <label for="username">
            <input name="username" tabindex="1" id="username" type="text">Username: 
        </label>
        <label for="password">
            <input name="password" tabindex="2" id="password" type="password">Password:
        </label>
        <label for="Name">
            <input name="name" tabindex="3" id="name" type="text">Name:
        </label>
        <label for="Surname">
            <input name="surname" tabindex="4" id="surname" type="text">Surname: 
        </label>
        <label for="address">
            <input name="address" tabindex="5" id="address" type="text">Address: 
        </label>
        <label for="email">
            <input name="email" tabindex="6" id="email" type="text">Email: 
        </label>
        <label for="phone">
            <input name="phone" tabindex="7" id="phone" type="text">Phone: 
        </label>
        <label for="zipcode">
            <input name="zipcode" tabindex="8" id="zipcode" type="text">Zip Code: 
        </label>
        <label for="city">
            <input name="city" tabindex="9" id="city" type="text">City: 
        </label>
        <label for="submit">
            <input name="submit" id="submit" tabindex="4" value="Log in" type="submit">
        </label>
    </fieldset>
</form>';
}

if(isset($_POST['submit']))
{
$errors = array();
$valid = 0;
if(empty($_POST['username']))
{
	$valid = 1;
	$errors['username'] = "Empty Username Field";
}
if(count($errors) > 0) 
{
	global $error_string;
      $error_string = implode('</li><li>', $errors);
      $error_string = "
        <div class='form_errors'>
          The following errors were encountered while processing your request:
          <ul><li>{$error_string}</li></ul>
        </div>
      ";
	show_register($errors);
}
else
{
	processform();
}

} else
{
show_register();
}

function processform()
{
echo "Registered";
}
?>

ok where i have if(count($errors) > 0 .. i would like that output to be inside the form after validation but i`m a little tired and i'm escaping something in the logical part. i would require some assistance.

Link to comment
https://forums.phpfreaks.com/topic/164160-form-trouble/
Share on other sites

Two things for one, inside of the show_register() function you set $error_string to = ''; which is no good, it will always equal nothing. Second you did not build the function to accept variables, so it will not use them. you need to start it like this.

 <?php
function show_register($errors){
    if(!isset($errors)){
        $error_string='';
    }
    else{
        $error_string=$errors;
    }
//etc.
?>

Link to comment
https://forums.phpfreaks.com/topic/164160-form-trouble/#findComment-865986
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.