2017-09-02 18 views
1

我試圖從後一種形式的隱藏輸入類型的隨機值..但它沒有發表任何值.. ..PHP mt_rand使用功能後的值不工作

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script type="text/javascript" src="jquery.form.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $('#images').on('change',function(){ 
     $('#multiple_upload_form').ajaxForm({ 
      target:'#images_preview', 
      beforeSubmit:function(e){ 
       $('.uploading').show(); 
      }, 
      success:function(e){ 
       $('.uploading').hide(); 
      }, 
      error:function(e){ 
      } 
     }).submit(); 
    }); 
}); 
</script> 
<form method="post" name="multiple_upload_form" id="multiple_upload_form" enctype="multipart/form-data" action="upload.php"> 
    <input type="hidden" name="randnum" id ="randnum" value="<?php echo mt_rand(1000,10000000); ?>" /> 
    <input type="hidden" name="image_form_submit" value="1"/> 
     <label>Choose Image</label> 
     <input type="file" name="images[]" id="images" multiple > 
    <div class="uploading none"> 
     <label>&nbsp;</label> 
     <img src="uploading.gif"/> 
    </div> 


</form> 

和這是upload.php的

if($_POST['image_form_submit'] == 1) 

{ 
    $rand = $_POST['randnum']; 

    $con=new PDO("mysql:host=localhost;dbname=newimg","root",""); 
    $images_arr = array(); 
    foreach($_FILES['images']['name'] as $key=>$val){ 
     $image_name = $_FILES['images']['name'][$key]; 
     $tmp_name = $_FILES['images']['tmp_name'][$key]; 
     $size  = $_FILES['images']['size'][$key]; 
     $type  = $_FILES['images']['type'][$key]; 
     $error  = $_FILES['images']['error'][$key]; 

     $target_dir = "uploads/"; 
     $target_file = $target_dir.$_FILES['images']['name'][$key]; 
     if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$target_file)){ 
      $images_arr[] = $target_file; 
      $addnew=$con->prepare("INSERT INTO attempt010(link,name,size,type)VALUES('$rand','$image_name','$size','$type')"); 
      $addnew->execute(); 
     } 
    } 
    $fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'"); 
    $fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC); 
    $fetch_imgid->execute(); 
    ?> 

我想每一個表單被張貼的時間存儲的隨機數...

+0

哎,請使用echo打印值例如。價值=「<?php回聲mt_rand(1000,10000)」;?> –

+0

我已經上傳了完整的代碼... –

+0

它張貼了其他圖片信息... –

回答

4

您需要echo由產生的價值象下面這樣: -

<input type="hidden" name="randnum" value="<?php echo mt_rand(1000,10000000); ?>" /> 

注: - 你的代碼與我的變更工作正常,如果我改變(form.js庫文件)一起: -

<script type="text/javascript" src="jquery.form.js"></script><!--local URL--> 

要: -

<script type="text/javascript" src="http://malsup.github.com/jquery.form.js"></script><!-- I used live URL --> 
+0

仍然無法正常工作...僅限發佈0 –

+0

我上傳了代碼... –

+0

不,它沒有顯示值... –

0

而不是

<input type="hidden" name="randnum" id ="randnum" 
     value="<?php echo mt_rand(1000,10000000); ?>" /> 

可以在PHP代碼提交表單

$rand=mt_rand(1000,10000000); 

這將幫助你生成密鑰每次提交後生成的文件上傳 if($_POST['image_form_submit'] == 1)

{ 
$rand = mt_rand(1000,10000000); 

$con=new PDO("mysql:host=localhost;dbname=newimg","root",""); 
$images_arr = array(); 
foreach($_FILES['images']['name'] as $key=>$val){ 
    $image_name = $_FILES['images']['name'][$key]; 
    $tmp_name = $_FILES['images']['tmp_name'][$key]; 
    $size  = $_FILES['images']['size'][$key]; 
    $type  = $_FILES['images']['type'][$key]; 
    $error  = $_FILES['images']['error'][$key]; 

    $target_dir = "uploads/"; 
    $target_file = $target_dir.$_FILES['images']['name'][$key]; 
    if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$target_file)){ 
     $images_arr[] = $target_file; 
     $addnew=$con->prepare("INSERT INTO attempt010(link,name,size,type)VALUES('$rand','$image_name','$size','$type')"); 
     $addnew->execute(); 
    } 
} 
$fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'"); 
$fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC); 
$fetch_imgid->execute(); 
+0

我想從表單中發佈值..我知道它會工作,如果我生成否。提交後 –

0
Please use try this, 
    Your ajax script is not working try this and check if opening and closing {} having error 



    <!DOCTYPE html> 
    <html lang="en"> 

    <head> 
     <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
     <meta charset="utf-8"> 
     <title>Bootstrap Starter</title> 
     <meta name="generator" content="Bootply" /> 
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
     <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"> 
     <!--[if lt IE 9]> 
       <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script> 
     <![endif]--> 
     <style type="text/css"> 
     /*CUSTOM CODE HERE*/ 
     </style> 

    </head> 

    <body> 

     <form method="post" name="multiple_upload_form" id="multiple_upload_form" enctype="multipart/form-data" action="upload.php"> 
     <input type="hidden" name="randnum" id ="randnum" value="<?php echo mt_rand(1000,10000000); ?>" /> 
     <input type="hidden" name="image_form_submit" value="1"/> 
      <label>Choose Image</label> 
      <input type="file" name="images[]" id="images" multiple > 
     <div class="uploading none"> 
      <label>&nbsp;</label> 
      <img src="uploading.gif"/> 
     </div> 

    </form> 

     <script type='text/javascript' src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> 
     <script type='text/javascript' src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function(){ 
     $('#images').on('change',function(){ 


      var formObj = $("#multiple_upload_form"); 
      var formURL = formObj.attr("action"); 
      var formData = new FormData(formObj[0]); 


      $('.uploading').show(); 
      $.ajax({ 
         url: formURL, 
         type: "POST", 
         data: formData, 
         contentType: false, 
         cache: false, 
         processData:false, 
         success: function(data, textStatus, jqXHR) 
         { 
          console.log(data); 
          $('.uploading').hide(); 

         }   
       }); 


     }); 
    }); 
    </script> 
    </body> 
    </html> 

    <?php 

    if($_POST['image_form_submit'] == 1) 

    { 
     $rand = $_POST['randnum']; 

     $con=new PDO("mysql:host=localhost;dbname=newimg","root",""); 
     $images_arr = array(); 
     foreach($_FILES['images']['name'] as $key=>$val){ 
      $image_name = $_FILES['images']['name'][$key]; 
      $tmp_name = $_FILES['images']['tmp_name'][$key]; 
      $size  = $_FILES['images']['size'][$key]; 
      $type  = $_FILES['images']['type'][$key]; 
      $error  = $_FILES['images']['error'][$key]; 

      $target_dir = "uploads/"; 
      $target_file = $target_dir.$_FILES['images']['name'][$key]; 
      if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$target_file)){ 
       $images_arr[] = $target_file; 
       $addnew=$con->prepare("INSERT INTO attempt010(link,name,size,type)VALUES('$rand','$image_name','$size','$type')"); 
       $addnew->execute(); 
      } 
     } 
     $fetch_imgid=$con->prepare("SELECT * FROM attempt010 where link='$rand'"); 
     $fetch_imgid->setFetchMode(PDO:: FETCH_ASSOC); 
     $fetch_imgid->execute(); 

     } 


    ?>