0

Im currently trying to develop a form that can work fully without JavaScript.

Basicly i want the user to be able to do the following:

  • Complete form
  • Submit form
  • Success(Update DB or what ever)
  • Fail (Go back to step 1 but with data from step 1 before already there)

I was thinking of doing the following for example:

<input type="text" value="<?php checkPost(name);?>"/>

Then in check post doing something like:

function checkPost($name){
    try{
       return  $_POST[name];
    }
    catch{
        return "";
    }
}

Would this stop the error:

Notice: Undefined index

Which means if the data wasnt posted then just set the value to "" but if it was posted then set the old value to it.

Is this the best way to do it can anyone tell me a smarter way to do this with my forms.

My form markup can be seen here: JS Fiddle

Lemex
  • 3,772
  • 14
  • 53
  • 87
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) — assuming that's what your question is about. – Álvaro González May 27 '13 at 14:29
  • Of course that won't work! A $_POST variable is represented by square brackets: `$_POST['name']` or to work with your script:`$_POST[$name]` Also more importantly, `function checkPost(name)` should be: `function checkPost($name)` The script doesn't know what 'name' is as it is not considered to be a php variable. – James May 27 '13 at 14:32
  • Sorry that was a typo sorted. – Lemex May 27 '13 at 14:34
  • Review my comment again, edited it :) – James May 27 '13 at 14:34
  • 1
    When you output to html, you should always use `htmlspecialchars($your_value)` to avoid having the output (potentially...) breaking the html. – jeroen May 27 '13 at 14:37

1 Answers1

1

It will not stop error.
Because error is not an Exception in this case.

This sounds much better:

function checkPost(name){
    return isset($_POST['name']) ? $_POST['name'] : '';
}

My opinion - it is very bad idea to write validation functions for each for field.
Read about Zend_Form.
You can include it in your project even if you are not using ZF.

Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46