开发者

PHP form - retain textbox values on submit

开发者 https://www.devze.com 2023-03-05 02:11 出处:网络
I have the following code for submitting a form - it doesn\'t do much at the moment but what I am looking to achieve is this:

I have the following code for submitting a form - it doesn't do much at the moment but what I am looking to achieve is this:

The textbox docTitle is a required field.

If the yourName textbox has text in it and the docTitle textbox is left blank, when submitted the required field message appears and the yourName textbox retains it's value.

I'm struggling with the part where the form retains the previous values after submitting.

Here's the code:

<?php
if(isset($_POST['submit'])) {
    if(empty($docTitle)) {
    } else {
        if ((($_FILES["file"]["type"] == "application/pdf")) && ($_FILES["file"]["size"] < 2000000) && ($_POST["docTitle"] > "")) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            }
                else
            {
                echo "Upload: " . $_FILES["file"]["name"];
                if (file_exists("upload/pdf/" . $_FILES["file"]["name"])) {
                    echo $_FILES["file"]["name"] . " already exists. ";
                }
                    else
                {
                    move_uploaded_file($_FILES['file']['tmp_name'],
                    "upload/pdf/" . $_FILES["file"]["name"]);
                }
            }
        }
        else
        {
            echo "Invalid file";
        } 
    }
}
?>

<form name="uploadAPdf" id="uploadAPdf" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<label for="text">Name: </label开发者_StackOverflow> <input type="text" name="yourName" id="yourName" /><br />
<label for="text">Document: </label> <input type="text" name="docTitle" id="docTitle" />
<?php if(isset($_POST['submit']) && empty($docTitle)) {
 echo " Document title must be filled in...";
 echo "<script type='text/javascript'>document.uploadAPdf.docTitle.focus();</script>";
} ?>
<br /> 
<label for="file">Select PDF to upload: </label> <input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>


Isn't this what you want:

<?php
function postValue($name, $alt = '') {
  return isset($_POST[$name]) ? $_POST[$name] : $alt;
}
?>
<label for="text">Name: </label> <input type="text" name="yourName" id="yourName" value="<?=htmlspecialchars(postValue('yourName'))?>" /><br />
<label for="text">Document: </label> <input type="text" name="docTitle" id="docTitle" value="<?=htmlspecialchars(postValue('yourName'))?>" />

That's (sort of) how most Forms work...

Maybe I'm missing something =)

edit
This is a function I used to use when I didn't use a PHP framework:

<?php
function ifsetor($var, $alt = '') {
  return isset($var) ? $var : $alt;
}
?>

which would be used like this:

<?php
$selectedOptions = ifsetor($_POST['options'], array());
?>

edit
Unrelated: you might want to put your form elements in a wrapper (instead of separating them with a <br>) like this:

<div class="form-element"><label>...</label><input .... /></div>


change your html input(s) code to:

 <input type="text" name="docTitle" id="docTitle" value="<?php echo htmlspecialchars($_POST['docTitle'];   ?>" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号