1

Before asking, here's my form picture: enter image description here

As you can see, I have 1 form with 2 tables, in each table, there are six inputs.

The input code in "Table Alat" and "Table Bahan" is like this:

<form method='post' action='p_input.php'>
    <table>
    <tr>
        <td>1.</td>
        <td><input type='text' name='nama[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='merk[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='kemasan[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='mhs[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='jml[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='hps[]' style='width:100%;' autocomplete='off'>
        <input type='hidden' name='jenis[]' value='alat' autocomplete='off'></td>
    </tr>
    </table>

    <table>
    <tr>
        <td>1.</td>
        <td><input type='text' name='nama[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='merk[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='kemasan[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='mhs[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='jml[]' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='hps[]' style='width:100%;' autocomplete='off'>
        <input type='hidden' name='jenis[]' value='bahan' autocomplete='off'></td>
    </tr>
    <table width='100%' align='center' style='margin-top:0;'>
    <tr>
    <td><input name='reset' type='reset' id='bersihkan' value='Reset'>
    <input name='submit' type='submit' id='ajukan' value='Send'></td>
    </tr>
    </table>
    </table>
    </form>

The difference in here is <input type='hidden' name='jenis[]' value='alat' autocomplete='off'>, for "Table Alat" the value is 'alat' for "Table Bahan" the value is 'bahan'(I have no trouble in here, just telling you).

This is my p_input.php script:

 <?php
       error_reporting(0);
       session_start();

        if(isset($_REQUEST['submit'])) {
        include "../conf/koneksi.php";
        $count = count($_POST['nama']);
        $jurusan = $_POST['jurusan'];
        $lab = $_POST['lab'];
        $materi = $_POST['materi'];
        $mahasiswa= $_POST['mahasiswa'];
        for($i = 0; $i < $count; $i++){
          $nama = mysql_real_escape_string($_POST['nama'][$i]);
          $merk = mysql_real_escape_string($_POST['merk'][$i]);
          $kemasan = mysql_real_escape_string($_POST['kemasan'][$i]);
          $mhs = mysql_real_escape_string($_POST['mhs'][$i]);
          $jml = mysql_real_escape_string($_POST['jml'][$i]);
          $hps = mysql_real_escape_string($_POST['hps'][$i]);
          $jenis = mysql_real_escape_string($_POST['jenis'][$i]);
          $sql2= "ALTER TABLE tb_usulan AUTO_INCREMENT = 1";
          mysql_query($sql2);
          $sql=mysql_query("INSERT INTO tb_usulan (nama,merk,kemasan,kebutuhan,jml_kebutuhan,hps,jenis,materi_praktikum)
           VALUES
           ('$nama', '$merk', '$kemasan', '$mhs', '$jml', '$hps','$jenis','$materi')") or die(mysql_error());

        }
        }
        ?>

And my question is: If I only fill input in "TABLE ALAT" and keep input in "TABLE BAHAN" empty, then when I click submit, I want only input in table alat to insert/post to database, and vice versa, if I only fill input in "TABLE BAHAN" and keep input in "TABLE ALAT" empty, then only input in "TABLE BAHAN" will insert/post to database.

UPDATE QUESTION: This is only one form and one submit button, and insert it to the same table at same time.

21
  • a good way to go around this would be to utilize AJAX since with php/html you can only submit one form per page, with ajax you can have it save to the database without a page refresh. Commented Sep 19, 2014 at 4:05
  • ahh sorry sorry, see the edit. I only use one form for this page. :) Commented Sep 19, 2014 at 4:08
  • 2
    Anyway, you can use if(!empty($_POST['nama'][$i])) to check whether that form is filled in. Commented Sep 19, 2014 at 4:14
  • 1
    Or use the hidden field as you already do. Commented Sep 19, 2014 at 4:15
  • 1
    Are they allowed to fill in both forms, or just one at a time? If they can fill in both, then make it one form, and do what I said with the empty() function. Commented Sep 19, 2014 at 4:22

3 Answers 3

2

just do a checking inside your for loop.

for($i = 0; $i < $count; $i++)
{
    //skip if nama is empty
    if($_POST["nama"][$i] == ""){continue;}

    $nama = mysql_real_escape_string($_POST['nama'][$i]);
    $merk = mysql_real_escape_string($_POST['merk'][$i]);
    $kemasan = mysql_real_escape_string($_POST['kemasan'][$i]);
    $mhs = mysql_real_escape_string($_POST['mhs'][$i]);
    $jml = mysql_real_escape_string($_POST['jml'][$i]);
    $hps = mysql_real_escape_string($_POST['hps'][$i]);
    $jenis = mysql_real_escape_string($_POST['jenis'][$i]);
    $sql2= "ALTER TABLE tb_usulan AUTO_INCREMENT = 1";
    mysql_query($sql2);
    $sql=mysql_query("INSERT INTO tb_usulan (nama,merk,kemasan,kebutuhan,jml_kebutuhan,hps,jenis,materi_praktikum)
    VALUES ('$nama', '$merk', '$kemasan', '$mhs', '$jml', '$hps','$jenis','$materi')") or die(mysql_error());
}
Sign up to request clarification or add additional context in comments.

2 Comments

the question is fine. if there's anything wrong with this answer, then it's already a design issue. If OP wants to only POST 1 row, he should not have share the form, unless he's playing with ajax.
Thanks for helping me friend, but my problem already solved, thanks for take your time for helping me.
1

Remember i added input field. so delete it before you apply the solution. as you already have input feild in your original form.

<table>
    <tr>
        <td>1.</td>
        <td><input type='text' name='nama1' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='merk1' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='kemasan1' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='mhs1' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='jml1' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='hps1' style='width:100%;' autocomplete='off'>
        <input type='hidden' name='jenis1' value='alat' autocomplete='off'></td>
    </tr>
    </table>

    <table>
    <tr>
        <td>1.</td>
        <td><input type='text' name='nama2' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='merk2' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='kemasan2' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='mhs2' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='jml2' style='width:100%;' autocomplete='off'></td>
        <td><input type='text' name='hps2' style='width:100%;' autocomplete='off'>
        <input type='hidden' name='jenis2' value='bahan' autocomplete='off'></td>
    </tr>
    </table>


  <input type='submit' name="submit">
</form>
</body>

Than where you are running the query only change that part.

if(!empty($_POST['name1']))
    {
        //Run First query
    }
    if(!empty($_POST['name2']))
    {
        //run your second query
    }

What this code will do it will check for the value. If the values are not empty than it will execute the query other wise it will not execute the query

and second is when you use the code you wont have any array so you need to remove the $count variable. and your foreach loop from the code.

And Third thing. you need to set the value after

if(isset($_REQUEST['submit'])) { include "../conf/koneksi.php";

For each and every feild like below as i am setting few

$name1=$_Post['name1']; $merk1=$_post['merk1'];

Do this for each and every value for both table.

And forth thing you need to change the value part in your query like your values going to be $name1, and so on for first query and for second query $name2 and so on.

Hope i explain this follow the step. and before following the steps. If you dont understand it ask me before you start applying them.

Thanks

12 Comments

i use $count and for loop because my table row is dynamic, so if i click "+"(as you can see in the image), it will add a new row.
@MuhammadFahmy if you use count because of this no worry. Solution will stand same. just some step going to change. You need to use to count for each plus sign lets $count1 and $count2. and yah than dont remove the for loop now which you were using in your original code. as you need count means for loop as well but include for loop in both of your if staement. if you dont know what i am talking about. let me know i will update my answer. Thanks
yes i think i understand what you means, so i must make loop for each query, right?
@MuhammadFahmy yes but remove that from the top. and include that in both of your if staement. which i posted in the answer
@MuhammadFahmy you muslim na so pray for me too. I am not a good muslim. and my email address is [email protected] you can contact me if you cant get the answer here. but i think you gonna get the answer but if you dont get it let me know
|
1

An easy solution would be to separate the tables to separate forms like this:

<form name=formOne>
   <table><!-- Alat etc. -->
   </table>
   <input type=submit>
</form>

<form name=formTwo>
   <table><!-- Behan etc. -->
   </table>
   <input type=submit>
</form>

then only the table for wich submit is pushed will get submited!

Edit To make it work with only one submit button (client side). Add the following jquery:

var lastForm;
$("form").click(function(){
     lastForm = this;
});

$("input[type=submit]").click(function(){
   $(lastForm).submit();
});

You then add the submit button outside of the form elements. But the nicest solution would be to check the fields serverside. (whitch should be done anyway since you can't really trust the end user).

But that has already been proposed in other answers!

7 Comments

the submit button is only one, so if when i click the submit button the table bahan is filled and table alat is empty, it will automatically only submit table bahan and ignore table alat.
You need to add one submit button, as seen in the example above!
But, it will make it less efficient, since my work place want only one sumbit button, and it was insert to the same table, anyway.
I didn't say it was the best solution, but it is a working one with almost no effort. The only other solution is to check wich form thats have been filled and then store values accordingly.
Yes, i think so too, if i make 2 submit button, it will be much easier.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.