텍스트 입력 및 파일 업로드 형식이 있습니다. 아래에 작성한 코드는 기본적으로 작동합니다 (별도의 질문에서 묻는 추가 기능이 필요합니다). 그러나이 코드를 작성하는 데 더 명확하고 간결한 방법이 있는지 궁금합니다. 나는 새로운 브랜드의 PHP와 어떤 도움을 주시면 감사하겠습니다.여러 입력 유형이있는 PHP 양식 유효성 검사
누군가가 양식을 제출하려고 시도하고 필드가 비어 있거나 선택한 파일 유형이 올바른 유형이나 크기가 아닌 경우 입력마다 오류 메시지를 표시하려고합니다. 아래의 코드에서 고유 한 메시지가 표시되지만 두 번째 생각에서이 코드를 더 잘 만들 수있는 방법이 있으면 "필수"라고 말할 수 있습니다.
jQuery 유효성 검사 플러그인을 사용해 보았는데 파일 업로드 (jQuery/Ajax/PHP에 대한 이해가 부족함)와 함께 작동하지 못했습니다. 그래서 이것은 제가 생각해 낸 해결책입니다.
<?php require_once('../scripts/lcoa.php'); ?>
<?php
if (isset($_GET['jobid'])) {
$jobid = $_GET['jobid'];
}
if (isset($_GET['jobtitle'])) {
$jobtitle = $_GET['jobtitle'];
}
//This is the directory where resumes will be saved
$target = "../careers/resumes/";
$target = $target . basename($_FILES['resume']['name']);
$resume=($_FILES['resume']['name']);
$type = ($_FILES['resume']['type']);
$extension = strtolower(substr($resume, strpos($resume, '.') + 1));
$size = ($_FILES['resume']['size']);
$max_size = 3145728;
$name = ($_POST['name']);
$email = ($_POST['email']);
$phone = ($_POST['phone']);
$jobid = ($_POST['jobid']);
$jobtitle = ($_POST['jobtitle']);
$cover = ($_POST['coverletter']);
if(isset($name)){
if (empty ($name)){
?>
<script type="text/javascript">
$(document).ready(function() {
$('#applicant-name').before('<p class="error">Please provide your full name. </p>');
});
</script>
<?php
}
}
if(isset($email)){
if (empty ($email)){
?>
<script type="text/javascript">
$(document).ready(function() {
$('#applicant-email').before('<p class="error">Please provide your email address. </p>');
});
</script>
<?php
}
}
if(isset($phone)){
if (empty ($phone)){
?>
<script type="text/javascript">
$(document).ready(function() {
$('#applicant-phone').before('<p class="error">Please provide a phone number. </p>');
});
</script>
<?php
}
}
//Writes the resume to the server
if (isset ($resume)) {
if (!empty ($resume)){
if(($extension=='doc'||$extension=='docx'||$extension=='txt'||$extension=='pdf')&&($type=='application/pdf'||'application/msword'||'application/vnd.openxmlformats-officedocument.wordprocessingml.document'||'text/plain')&&$size<=$max_size) {
if(move_uploaded_file($_FILES['resume']['tmp_name'], $target)) {
//Writes the information to the database
$insertSQL = "INSERT INTO applicants (id, name, email, phone, jobid, jobtitle, coverletter, resume) VALUES ('','".$_POST['name']."','".$_POST['email']."','".$_POST['phone']."','".$_POST['jobid']."','".$_POST['jobtitle']."','".$_POST['coverletter']."','".$resume."')";
mysql_select_db($database_lcoa, $lcoa);
$Result1 = mysql_query($insertSQL, $lcoa) or die(mysql_error());
//Tells you if its all ok
echo "<div id='confirm-app'><p>Thank you for submitting your application. Resumes submitted will be reviewed to determine qualifications that match our hiring needs.<br /><br /> If you are selected you will be contacted by a member of our recruiting team.</p><br /><br /><a href='../careers/job-postings.php'>Return to current opportunities</a></div>";
}
}
else {
//Gives and error if its not
echo "<p style='color: #6D6E71; font-family: Arial,Helvetica,sans-serif; font-size: 13px;'>We accept resumes in <strong>.doc</strong>, <strong>.docx</strong>, <strong>.pdf</strong>, or <strong>.txt</strong> formats, 3MB or less. Please <a href='javascript:history.back(-1);'>go back</a> to upload a file that meets these requirements.<br /><br />If you continue to experience errors, please report them.</p>";
die();
}
}
else {
?>
<script type="text/javascript">
$(document).ready(function() {
$('#upload-resume').before('<p class="error">Please select a file to upload. </p>');
});
</script>
<?php
}
}
?>
을 만드는 희망 등 이메일. 그래서 아니오라고 말하지만, 거기에 프레임 워크가 정리되어 있다고 확신합니다. 그러나 나는 보통 PHP가 때때로 엉망이 될 수 있다는 것을 귀찮게 받아들이지 않습니다. –
먼저 http://buildinternet.com/2008/12/how-to-validate-a-form-complete-with-error-messages-using-php-part-1/와 같은 자습서를 사용해보십시오. http : //www.daniweb.com/web-development/php/threads/186170/php-file-upload-validation-before-the-file-uploads –
@ dianuj 감사합니다. 튜토리얼의 논리가 꽤 많은 것 같습니다. 나는 그랬다. 나는이 솔루션을 제시하기 위해 다양한 자습서를 사용했다. – surfbird0713