0

How can I get values from the added input field into my database? When I run this code the table shows "array" instead of the values entered..

Javascript to add input field:

<script>
$(document).ready(function() {
    var max_fields      = 25; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initial text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><label for="no_telefon[]">No.Telefon: </label><input type="text" name="no_telefon[]" id="no_telefon[]" class="required input_field"><label for="lokasi[]">Lokasi: </label><input type="text" name="lokasi[]" id="lokasi[]"  class="required input_field"><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

</script>

the Input field form:

                <fieldset>

                <div class="input_fields_wrap">
                <h3 class="add_field_button"><a href="">Add More Fields</a></h3>  



                <label for="no_telefon[]">No.Telefon:</label> <input type="text"  id="no_telefon[]" name="no_telefon[]" class="required input_field" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" required/> 
                <label for="lokasi[]">Lokasi:</label> <input type="text" id="lokasi[]" name="lokasi[]" class="required input_field" required/>


                </div>

and the PHP file to insert data into the database:

<?php
require("dbase.php"); 

if ($_POST) {

    $id_akaun           = isset($_POST['id_akaun'])         ? $_POST['id_akaun'] : '';
    $daerah             = isset($_POST['daerah'])           ? $_POST['daerah'] : '';
    $kategori_akaun     = isset($_POST['kategori_akaun'])   ? $_POST['kategori_akaun'] : '';
    $bahagian           = isset($_POST['bahagian'])         ? $_POST['bahagian'] : '';
    $jenis              = isset($_POST['jenis'])            ? $_POST['jenis'] : '';
    $no_telefon         = isset($_POST['no_telefon'])       ? $_POST['no_telefon'] : '';
    $lokasi             = isset($_POST['lokasi'])           ? $_POST['lokasi'] : '';
    $id                 = isset($_POST['id'])               ? $_POST['id'] : '';


    $sql = mysql_query("INSERT INTO maklumat_akaun VALUES ('', '$id_akaun' , '$daerah' , '$kategori_akaun' , '$bahagian' )");
    $sql = mysql_query("INSERT INTO detail_akaun VALUES   ('', '$jenis' , '$no_telefon' , '$lokasi', '".mysql_insert_id()."' )");





    echo "<script type='text/javascript'> alert('AKAUN BERJAYA DIDAFTARKAN')</script> ";
    echo "<script type='text/javascript'>window.location='lamanutama.php'</script>";
}


?>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
itsfawwaz
  • 117
  • 1
  • 8

2 Answers2

0

Array data can't be saved in SQL because it's a php data type, and cannot be easily converted to a string.

So you will need to do the conversion by yourself.

A quick example:

$no_telefon_string = "";
foreach($no_telefon as $telefon)
{
    $no_telefon_string .= $telefon.",";
}
$no_telefon_string = rtrim($no_telefon_string, ",");

Now you can insert the $no_telefon_string variable into the database.

To turn $no_telefon_string back into an array you can use:

$no_telefon = explode($no_telefon_string, ",");

$no_telefon will now be the array you originally got from the form post.

MicroParsec
  • 229
  • 1
  • 7
0

You need to use a loop to process all the no_telefon and lokasi fields:

$sql = mysql_query("INSERT INTO maklumat_akaun VALUES ('', '$id_akaun' , '$daerah' , '$kategori_akaun' , '$bahagian' )");
$akaun_id = mysql_insert_id();
foreach ($no_telefon AS $i => $telefon) {
    $sql = mysql_query("INSERT INTO detail_akaun VALUES   ('', '$jenis' , '$telefon' , '$lokasi[$i]', '$akaun_id' )");
}

This will create a separate row in detail_akaun for each pair of no_telefon and lokasi.

BTW, you're creating duplicate IDs with id="no_telefon[]" and id="lokasi[]". IDs are supposed to be unique. All your labels with for="no_telefon[] and for="lokasi[] will be attached to the fields in the first row, not the one after it. Instead of using IDs and for, try wrapping your labels around the inputs:

<label>No.Telefon: <input type="text" name="no_telefon[]" class="required input_field" onkeyup="this.value=this.value.replace(/[^0-9.]/g,'')" required/></label>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The name of the variable has nothing to do with the name of the column in the database. You should use the wrap label method for any input tag that doesn't have a unique ID. You can use it for other inputs as well if you want. See http://stackoverflow.com/questions/18432376/what-does-for-attribute-do-in-label-tags/18432439#18432439 – Barmar Oct 17 '14 at 01:33