2016-01-22 62 views
0

我使用JavaScript時點擊添加按鈕顯示多個文本框。但我不知道如何將這些文本框值存儲在數據庫表單列中。這裏我附加了我的表單輸入編碼和JavaScript添加數量的文本框。在提交我的表單之後,它將像Array這樣的東西存儲到我的表中。多個文本框中的值存儲在同一列中php

<?php 
if(isset($_POST['submit'])) 
{ 
Include 'db.php'; 
//$digits = 5; 
//$staff_id=STAF.rand(pow(10, $digits-1), pow(10, $digits)-1); 
$fromlocation = $_POST['fromlocation']; 
$fromlatitude = $_POST['fromlatitude']; 
$fromlongitude = $_POST['fromlongitude']; 
$tolocation = $_POST['tolocation']; 
$tolatitude = $_POST['tolatitude']; 
$tolongitude = $_POST['tolongitude']; 
// $routes = $_POST['routes']; 
//$routesmore = $_POST['routes_more']; 
$date=date('Y-m-d H:i:s'); 
    $status=1; 
//$usertype=1; 

    $count = $_POST['count']; 

    for($i = 0 ; $i < $count ; $i++) 

     { 
      //$count++; 
      $routesmore = $_POST['routes_more']; 

      $routesmore2 = explode('.', $routesmore[0]); 
     } 



    $query = mysqli_query($connect,"INSERT INTO `motorpark-db`.`tbl_route` (`from_location`, `from_latitude`, `from_longitude`, `to_location`, `to_latitude`, `to_longitude`, `route1`, `status`, `created_date`) VALUES ('$fromlocation', '$fromlatitude', '$fromlongitude', '$tolocation', '$tolatitude', '$tolongitude', '$routesmore2', '$status', '$date');"); 

if($query) 
    { 
    header('location:create_route.php#managepart'); 
    } 
    else 
    { 
    header('location:create_staff.php'); 
    } 
    } 

    ?> 

我的輸入框:

<div class="col-lg-8" id="img_upload"> 
        <!-- <input type="text" id="file0" name="routes" style="background:none;width:185px;"> --> 
        <div id="divTxt"></div> 
      <p><a onClick="addproductimageFormField(); return false;" style="cursor:pointer;width:100px;" id="add_img_btn" class="btn btn-primary">Add Route</a></p> 
        <input type="hidden" id="aid" value="1"> 
        <input type="hidden" id="count" name="count" value="0"> 

我的javascript:

<script type="text/javascript"> 



    function addproductimageFormField() 
    { 
     var id = document.getElementById("aid").value; 
     var count_id = document.getElementById("count").value;  
     if(count_id < 2) 
      { 
      document.getElementById('count').value = parseInt(count_id)+1; 
       var count_id_new = document.getElementById("count").value; 
       jQuery.noConflict() 
       jQuery("#divTxt").append("<div id='row" + count_id + "' style='width:100%'><fieldset class='gllpLatlonPicker'><label for='text- input'>Stop</label><span style='color:red;'> *</span><input type='text' class='gllpSearchField' name='routes_more"+count_id+"' id='file"+count_id_new+"' /></fieldset>&nbsp;&nbsp<a href='#' onClick='removeFormField(\"#row" + count_id + "\"); return false;' style='color:#F60;' >Remove</a></div>");  
     jQuery('#row' + id).highlightFade({speed:1000 }); 
       id = (id - 1) + 2; 
       document.getElementById("aid").value = id; 
       } 
       } 

     function removeFormField(id) 
     { 
     //alert(id); 
     var count_id = document.getElementById("count").value; 
     document.getElementById('count').value = parseInt(count_id)-1; 
     jQuery(id).remove(); 
      } 
     </script> 

回答

0

更改打印JS - 附加routes_more[]jQuery("#divTxt").appendroutes_more'+count+'的地方。

<script type="text/javascript"> 
    function addproductimageFormField() 
    { 
     var id = document.getElementById("aid").value; 
     var count_id = document.getElementById("count").value;  
     if(count_id < 2) 
     { 
      document.getElementById('count').value = parseInt(count_id)+1; 
      var count_id_new = document.getElementById("count").value; 
      jQuery.noConflict() 
      jQuery("#divTxt").append("<div id='row" + count_id + "' style='width:100%'><fieldset class='gllpLatlonPicker'><label for='text- input'>Stop</label><span style='color:red;'> *</span><input type='text' class='gllpSearchField' name='routes_more[]' id='file"+count_id_new+"' /></fieldset>&nbsp;&nbsp<a href='#' onClick='removeFormField(\"#row" + count_id + "\"); return false;' style='color:#F60;' >Remove</a></div>");  
      jQuery('#row' + id).highlightFade({speed:1000 }); 
      id = (id - 1) + 2; 
      document.getElementById("aid").value = id; 
     } 
    } 

    function removeFormField(id) 
    { 
     //alert(id); 
     var count_id = document.getElementById("count").value; 
     document.getElementById('count').value = parseInt(count_id)-1; 
     jQuery(id).remove(); 
    } 
</script> 

變化PHP代碼 - 查找routes_more文本框的總數。並據此做。 (不需要檢查你的html代碼中有多少計數。)

<?php 
if(isset($_POST['submit'])) 
{ 
    include 'db.php'; 
    //$digits = 5; 
    //$staff_id=STAF.rand(pow(10, $digits-1), pow(10, $digits)-1); 
    $fromlocation = $_POST['fromlocation']; 
    $fromlatitude = $_POST['fromlatitude']; 
    $fromlongitude = $_POST['fromlongitude']; 
    $tolocation = $_POST['tolocation']; 
    $tolatitude = $_POST['tolatitude']; 
    $tolongitude = $_POST['tolongitude']; 
    // $routes = $_POST['routes']; 
    //$routesmore = $_POST['routes_more']; 
    $date=date('Y-m-d H:i:s'); 
    $status=1; 
    //$usertype=1; 

    //For Routes More 
    $totalRoutesCount = sizeof($_POST['routes_more']); 
    $totalRoutes=""; 
    for($i=0;$i<$totalRoutesCount;$i++) 
    { 

      $totalRoutes = $totalRoutes.$routesmore[$i].","; 
    } 
    $totalRoutes = rtrim($totalRoutes, ','); 

    $query = mysqli_query($connect,"INSERT INTO `motorpark-db`.`tbl_route` (`from_location`, `from_latitude`, `from_longitude`, `to_location`, `to_latitude`, `to_longitude`, `route1`, `status`, `created_date`) VALUES ('$fromlocation', '$fromlatitude', '$fromlongitude', '$tolocation', '$tolatitude', '$tolongitude', '$totalRoutes', '$status', '$date');"); 

    if($query) 
    { 
     header('location:create_route.php#managepart'); 
    } 
    else 
    { 
     header('location:create_staff.php'); 
    } 
} 

?> 
0

HTML:

<input type="text" 
    id="file0" name="routes[]" 
    style="background:none;width:185px;"> 

PHP: INSERT查詢:

'routes'(BD column)  = serialize($post['routes']); 

顯示時間: 反序列化列路由和使用foreach循環

相關問題