Jump to content

Recommended Posts

Ok lets say your form is like this:

 

<form>
<input type="text" name="testbox" value="<?php echo $_POST['testbox']; ?>">
<input type="submit" value="submit" name="submit">
</form>

 

Then your PHP will be like this

 

if (isset($_POST['submit'])) {                      //This checks to see if the submit button has been clicked
if (isset($_POST['testbox'])) {                    //This checks to see if the "testbox" input field contains something
$testbox = $_POST['testbox'];
if (strlen($testbox) < 1) {                           //Checks to see if the length of the value in $testbox is less that 1 character
echo "You must enter something in test box";
}
} else {
echo "Test box must have an value";
}
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/137295-fill-in-all-fields/#findComment-717491
Share on other sites

Is it the same as using javascript, though i tried it but it did not work cos i'm not very sure of javascript..

 

function validate_required(field,alerttxt)

{

with (field)

{

  if (value==null||value=="")

  {

  alert(alerttxt);return false;

  }

  else

  {

  return true;

  }

}

}

Link to comment
https://forums.phpfreaks.com/topic/137295-fill-in-all-fields/#findComment-717653
Share on other sites

Although JavaScript is a great userfriendly way to do some pre-validation, you should ALWAYS include validation server-side as well. Not everyone has JS enabled and someone could easily bypass any JavaScript validation to do some malicious things to your site.

 

There's no problem in adding javascript valiadtion as long as it is on top of the server-side validation.

Link to comment
https://forums.phpfreaks.com/topic/137295-fill-in-all-fields/#findComment-717708
Share on other sites

I used these instead , not the javascript as i know nuts about it..

What i'm trying to achieve now is to keep the contents of all the required fields in the form  after it has been filled wrongly  ?

and there should be a pop up error box saying " this field is not entered"...

 

Ok lets say your form is like this:

 

<form>
<input type="text" name="testbox" value="<?php echo $_POST['testbox']; ?>">
<input type="submit" value="submit" name="submit">
</form>

 

Then your PHP will be like this

 

if (isset($_POST['submit'])) {                      //This checks to see if the submit button has been clicked
if (isset($_POST['testbox'])) {                    //This checks to see if the "testbox" input field contains something
$testbox = $_POST['testbox'];
if (strlen($testbox) < 1) {                           //Checks to see if the length of the value in $testbox is less that 1 character
echo "You must enter something in test box";
}
} else {
echo "Test box must have an value";
}
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/137295-fill-in-all-fields/#findComment-717757
Share on other sites

That code should do that. But, here is an example page that will combine the PHP and JavaScript validation. You can use it as a guide. Either turn off JS or take out the onsubmit trigger from the form to test the PHP validations.

 

<?php

  //Create an array to hold the errors
  $errors = array();

  //Create post values as var. This is useful in case
  //you need to run stripslashes or some other process.
  $name = $_POST['name'];
  $phone = $_POST['phone'];
  $email = $_POST['email'];

  //Validate the form data
  if (isset($_POST))
  {
    if (empty($name))
    {
      $errors[] = 'Name cannot be empty.';
    }
    if (empty($phone))
    {
      $errors[] = 'Phone cannot be empty.';
    }
      if (empty($email))
    {
      $errors[] = 'Email cannot be empty.';
    }

    if (!count($errors))
    {
      //Process the form data
      include('processing_page.php');
      //Reload page to clear POST values (in case user refreshes page)
      header('Location: '.$_SERVER[php_SELF])
    }
    else
    {
      $errorMsg = "The following errors occured:<br />\n";
      $errorMsg = "<ul>\n";
  foreach($errors as $error)
      {
        $errorMsg .= "<li>{$error}</li>";
  }
      $errorMsg .= "</ul>\n";
    }
  }
?>
<html>
<head>
<script type="text/javascript">

//Add javascript prototype to trim string
String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/g,'');
}

function validate(formObj)
{
    //Create an array to hold all the errors
    errors = new Array();

    //Perform each validation
    if (formObj['name'].value.trim()=='')
    {
        errors[errors.length] = 'Name cannot be empty.';
    }
    if (formObj['phone'].value.trim()=='')
    {
        errors[errors.length] = 'Phone cannot be empty.';
    }
    if (formObj['email'].value.trim()=='')
    {
        errors[errors.length] = 'Email cannot be empty.';
    }

    //If errors dispay message and return false
    if (errors.length != 0)
    {
        var errorMsg = 'The following errors occured:\n';
        for (var i=0; i<errors.length; i++)
        {
            errorMsg += '\n - ' + errors[i];
        }
        alert(errorMsg);
        return false;
    }

    //No errors, return true
    return true;
}
</script>
</head>

<body>
Please enter your data. All fields are required.
<br />
<?php echo $errorMsg; ?>
<br />
<form name="test" onsubmit="return validate(this);" method="POST" action="">
Name: <input type="text" name="name" value="<?php echo $name; ?>" /><br />
Phone: <input type="text" name="phone" value="<?php echo $phone; ?>" /><br />
Email: <input type="text" name="email" value="<?php echo $email; ?>" /><br />
<br />
<button type="submit">Proceed</button>
</form>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/137295-fill-in-all-fields/#findComment-717936
Share on other sites

Thanks for the example as it was a really great help!  ;D

I'd like to clarify smtg..For radio buttons..i put this but it din work, though it worked fine for all input type as text box.

 

<input type="radio" name="gender" value="<?php echo $gender; ?>" />

 

if (formObj[gender].value.trim()=='')

    {

        errors[errors.length] = Gender cannot be empty.';

}

 

Link to comment
https://forums.phpfreaks.com/topic/137295-fill-in-all-fields/#findComment-718515
Share on other sites

Is there any1 who can help me wif this cos i cant move on being stuck on this too long..      :-\

I 'd like to use radio button but is this the correct code? for it din work for mine...

 

if (formObj[gender].value.trim()=='')
    {
        errors[errors.length] = Gender cannot be empty.';
      }

Link to comment
https://forums.phpfreaks.com/topic/137295-fill-in-all-fields/#findComment-718822
Share on other sites

if (formObj[gender].value.trim()=='')
    {
        errors[errors.length] = Gender cannot be empty.';
	}

 

Well, first off I will state that you should just pre-select one of the radio options since the user can only change the selected option and cannot unselect all of the options. But, there are some cases where you do not want to pre-select an option. The problem is that a radio GROUP is an array of elements - each with it's own value (however, if there is only one element in the group then it is not an array). You need to determine which element is CHECKED and return the value of that element.

 

Add this function to your page:

function radioGroupValue(groupObj)
{
    //Check if radio group has multiple options (i.e. is an array)
    if (groupObj.length)
    {
        //Radio group has multiple options
        //Iterrate through each option
        for (var i=0; i<groupObj.length; i++)
        {
            //Check if option is checked
            if (groupObj[i].checked)
            {
                //Return value of the checked radio button
                return groupObj[i].value;
            }
        }
    }
    else
    {
        //Radio group only has one option
        if (groupObj.checked)
        {
            return groupObj.value;
        }
    }

    //No ption was selected
    return false;
}

 

Then change your validation to this:

    if (radioGroupValue(formObj['gender'])!==false)
    {
        errors[errors.length] = Gender cannot be empty.';
    }

Link to comment
https://forums.phpfreaks.com/topic/137295-fill-in-all-fields/#findComment-718908
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.