简体   繁体   中英

multiple textbox values storing in same column in php

I an using Javascript when click add button to show multiple text box. but i don't how to store these text box values in database table single column. here i attached my form input coding and javascript for add number of textbox. after submit my form it stores somthing like Array into my table.

<?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');
    }
    }

    ?>

my input box:

<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">

My 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>  

HTML :

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

PHP: INSERT Query:

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

Display Time: unserialize the column routes and print with foreach loop

Change In JS - Append routes_more[] in jQuery("#divTxt").append in place of routes_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> 

Change in PHP Code - Find total count of routes_more textbox. And do accordingly. (No Need of checking how much count was there in your html code.)

<?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');
    }
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM