doulan9287 2015-01-31 12:14
浏览 12
已采纳

即使禁用了文件上载/字段元素,也会插入PHP脚本

Hi I can't seem to pinpoint what's wrong. I have a form with elements. Could you check the script? I just know it lacks something but a noob like me just doesn't know. Basically when the Smart Money radio button is selected, the BPI is disabled,and vice versa. It should insert the data inputs in the db. It works fine if the selected radio btn is BPI but if Smart money was selected and the user inputs data and BPI/file upload disabled, it doesn't insert anything in the db. Can you please show me what to do I think the scripts are kinda placed wrong, because the query is below the file upload script. I think it doesn't insert anything when there is nothing to upload/that option is disabled. The file upload script interferes I guess.

PHP:

if(isset($_FILES['filename'])){
    $errors = array();
    $file_name = $_FILES['filename']['name'];
    $file_size =$_FILES['filename']['size'];
    $file_tmp =$_FILES['filename']['tmp_name'];
    $file_type=$_FILES['filename']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));


    $expensions= array("jpeg","jpg","png");         
    if(in_array($file_ext,$expensions)=== false){
        $errors[]="extension not allowed, please choose a JPEG or PNG file.";
    }
    if($file_size > 2097152){
        $errors[]='File size must be excately 2 MB';
    }          

    // if no error...     
    if (empty($errors)==true) {

        // upload the file...
        move_uploaded_file($file_tmp,"uploads/".$file_name);

        $servername = "localhost";
        $username = "root";
        $password = " ";
        $dbname = "admin";

        // create new record in the database
        include ("dbinfo.php");

        mysql_query("INSERT INTO payment_form (Tracking, date, mode, ContactNo, totalsent, datesent, filename) VALUES ('$transactionNo', NOW(), '$rad', '$contactNo', '$totalSent', '$dateSent', '$file_name')") ;

        header('Location: paymentform_success.php');
    }else{
        print_r($errors);
    }
}

Form:

<form name="form" method="POST" enctype="multipart/form-data">
<table width="416" height="245" border="1" align="center">
<tr>
<td colspan="2">Transaction No: <input type="text" name="transaction_no" id="transaction_no" /> </td>
</tr>
<tr>
<td colspan="2" align="center">Please select the mode of payment</td>
</tr>
<tr>
<td width="183" align="center"><input name="rad" type="radio" onclick="enableField(this)" value="Smart Money"> 
Smart Money</td>
<td width="201" align="center"><input name="rad" type="radio" onclick="enableField(this)" value="BPI"> BPI Bank Deposit</td>
</tr>
<tr>
<td align="center"><input name="contactno" type="text" disabled="disabled" id="contactno"></td>
<td align="center"><input name="filename" type="file" id="filename" disabled="disabled"/></td>
</tr>
<tr>
<td>Total amount sent:</td>
<td>&nbsp;<input type="text" name="totalsent" id="totalsent" /></td>
</tr>
<tr>
<td>Date sent:</td>
<td>&nbsp;<input type="text" name="datesent" id="datesent" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" id="submit" value="Submit" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form" />

</form>

JS for disable/enable

<script type="text/javascript">
function enableField(obj){
    var form=obj.form;
    var txtNames=['contactno','filename'], f;
    var rads=document.getElementsByName(obj.name), r, i=0;
    while(r=rads[i++]){
        f=form[txtNames[i-1]];
        if(r.checked){
            f.removeAttribute('disabled');
            f.focus();
        }
        else{
            f.value='';
            f.setAttribute('disabled','disabled')
        }
    }
}
</script>
  • 写回答

1条回答 默认 最新

  • dongshuo9350 2015-01-31 12:35
    关注

    Do the INSERT outside the if(isset($_FILES['filename'])) block.

    if (isset($_POST['submit'])) {
        $errors = array();
        if (isset($_FILES['filename'])) {
            $file_name = $_FILES['filename']['name'];
            $file_size =$_FILES['filename']['size'];
            $file_tmp =$_FILES['filename']['tmp_name'];
            $file_type=$_FILES['filename']['type'];   
            $file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));
    
            $expensions= array("jpeg","jpg","png");         
            if(in_array($file_ext,$expensions)=== false){
                $errors[]="extension not allowed, please choose a JPEG or PNG file.";
            }
            if($file_size > 2097152){
                $errors[]='File size must be excately 2 MB';
            }          
    
            // if no error...     
            if (empty($errors)==true) {
    
                // upload the file...
                move_uploaded_file($file_tmp,"uploads/".$file_name);
    
            }else{
                print_r($errors);
            }
        } else {
            $file_name = '';
        }
    
        if (empty($errors)) {
            $servername = "localhost";
            $username = "root";
            $password = " ";
            $dbname = "admin";
    
            // create new record in the database
            include ("dbinfo.php");
    
            $transactionNo = $_POST['transaction_no'];
            $rad = $_POST['rad'];
            $contactNo = $_POST['contactno'];
            $totalSent = $_POST['totalsent'];
            $dateSent = $_POST['datesent'];
    
            mysql_query("INSERT INTO payment_form (Tracking, date, mode, ContactNo, totalsent, datesent, filename) VALUES ('$transactionNo', NOW(), '$rad', '$contactNo', '$totalSent', '$dateSent', '$file_name')") ;
    
            header('Location: paymentform_success.php');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备