jeffman Posted February 9, 2013 Share Posted February 9, 2013 Here is a different kind of function. If I understand your needs, it should work. It requires a few changes to your HTML, which I show here: <fieldset id="field"> <input type="text" name="incident" /> <select name="sev"> <option value="none0"></option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> </select> <select name="status"> <option value="###"></option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">######</option> <option value="###">###</option> </select> <textarea rows="2" cols="30" name="description_tic"></textarea></fieldset><input type="button" value="Add" id="addButton" /> The four form controls that you want duplicated are inside a fieldset. The function below clones the fieldset and its contents. Then it changes the names of the new form controls so that when the form is submitted, your server script can tell which data is which. There is another way to change the names since you are using PHP. Ask me if you want. It might be better. window.onload = function () { document.getElementById("addButton").onclick = cloneFieldSet;}var counter = 0;function cloneFieldSet () { counter++; var theField = document.getElementById("field"); var theClone = theField.cloneNode(true); var elements = theClone.elements; for (var el in elements) { elements[el].name += counter.toString(); } theClone.id += counter.toString(); theField.form.insertBefore(theClone, this);} 1 Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 9, 2013 Author Share Posted February 9, 2013 After clicking on the button nothing is happen no new fieldset is added on the page. <script>window.onload = function () { document.getElementById("addButton").onclick = cloneFieldSet;}var counter = 0;function cloneFieldSet () { counter++; var theField = document.getElementById("field"); var theClone = theField.cloneNode(true); var elements = theClone.elements; for (var el in elements) { elements[el].name += counter.toString(); } theClone.id += counter.toString(); theField.form.insertBefore(theClone, this);}</script><fieldset id="field"> <input type="text" name="incident" /> <select name="sev"> <option value="none0"></option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> </select> <select name="status"> <option value="###"></option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">######</option> <option value="###">###</option> </select> <textarea rows="2" cols="30" name="description_tic"></textarea></fieldset><input type="button" value="Add" id="addButton" /> Link to comment Share on other sites More sharing options...
jeffman Posted February 9, 2013 Share Posted February 9, 2013 (edited) The fieldset has to go in your form. AND the button. Edited February 9, 2013 by Deirdre's Dad 1 Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 9, 2013 Author Share Posted February 9, 2013 Thanks a Ton "Deirdre" Its seem to be working, let me integrate in php, and then i will let you know about it. Thanks a Ton again Sir :Happy: Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 10, 2013 Author Share Posted February 10, 2013 (edited) When ever i try to insert data into my mysql database nothing is happen mean data is not inserting into the table.Please have a look into my code I have created a two file 1) index.php2) validation.php => This is used for validation,database connection string and mysql query [index.php] <?php include_once('validation.php'); ?><html><body><head><title>Unix Daily Turnover</title></head> <script>window.onload = function (){ document.getElementById("addButton").onclick = cloneFieldSet;}var counter = 0;function cloneFieldSet (){ counter++; var theField = document.getElementById("field"); var theClone = theField.cloneNode(true); var elements = theClone.elements; for (var el in elements) { elements[el].name += counter.toString(); } theClone.id += counter.toString(); theField.form.insertBefore(theClone, this);} </script><form action="index.php" method="post"> <fieldset id="field"> <input type="text" name="incident" /> <select name="sev"> <option value="none0"></option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> </select> <select name="status"> <option value="###"></option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">###</option> <option value="###">######</option> <option value="###">###</option> </select> <textarea rows="2" cols="30" name="description_tic"></textarea></fieldset><input type="button" value="Add" id="addButton" /></center><input type="submit" value="submit" id="submit" /></center></form></body></html> validation.php <?phpdate_default_timezone_set('Asia/Calcutta');$date=date("m/d/Y"); if(isset($_POST['submit'])){ if(isset($_POST['submit'])) include("connection.php"); $incidentid=$_POST['incident']; $severity=$_POST['sev']; $status =$_POST['status']; $description_ticket=$_POST['description_tic']; $selectcondition = "SELECT * FROM test where DATE='".$date."'";//Select Query Which fetch the DATE $check=mysql_query($selectcondition,$con); $fetch=mysql_num_rows($check); if($fetch >=1) { echo "<br><b><br><font color='grey'>NOTICE<br>TURNOVER For The Same Date Is Already Exit"; echo "<br>Please Check 'Search' Or Update 'Page'. </b></font>"; } else { $sql = mysql_query("INSERT INTO test (DATE,INCIDENTID,SEVERITY,STATUS,TICKETDES) VALUES ('$date','$incidentid','$severity','$status','$description_ticket')") or die ("Database Connection Error" .mysql_error()); if($sql) { echo "<br><b> <font color='green'>Turnover for $date Submitted Successfully</b></font>"; } } }?> Edited February 10, 2013 by Ashish Sood Link to comment Share on other sites More sharing options...
jeffman Posted February 10, 2013 Share Posted February 10, 2013 What message is getting echoed? Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 10, 2013 Author Share Posted February 10, 2013 No error message upon submitting.. i also checked by making some deliberate error into my validation.php file but still i am getting no error message i think data is not posting into validation.php file ... I am trying to search the problem. Link to comment Share on other sites More sharing options...
jeffman Posted February 10, 2013 Share Posted February 10, 2013 Try adding a name="submit" attribute to your submit button. I think your test for $_POST['submit'] is failing, and you have not added any way of printing a message when this happens. also I do not understand why index.php includes validation.php. Have you tried NOT including validation.php and submitting the form directly to validation.php instead of to index.php? Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 10, 2013 Author Share Posted February 10, 2013 oppsss Correct its working sir thanks one problem when i tried to add more fieldsets on the page and click on the submit button then only the first fieldset data is insert rest is not. hope you got my problem... Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 10, 2013 Author Share Posted February 10, 2013 I troubleshoot and got to know that when ever i click on add button its copy the row and its data from earlier one, that why only the single row data is inserting . Link to comment Share on other sites More sharing options...
dsonesuk Posted February 10, 2013 Share Posted February 10, 2013 Because every added duplicate input element has the same name for name attribute, and so it will read the first one only, you could use name="sev[]" which will send it as array of all these values, entered. For the copying of values from previous you will have to clear these from the new when added. Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 10, 2013 Author Share Posted February 10, 2013 i tried & check this time when i click on the add button value is not duplicated into the new row, but after click on the submit button 'Array' string is inserted into the table not the original value. Link to comment Share on other sites More sharing options...
dsonesuk Posted February 10, 2013 Share Posted February 10, 2013 (edited) well of course with any array passed such as checkbox especially, to split them up you would have to a foreach or implode to group together in one string, depending in which way you wish to store these values. Edited February 10, 2013 by dsonesuk Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 10, 2013 Author Share Posted February 10, 2013 Sorry i cant understand, its good for me if you can explain with example .. Link to comment Share on other sites More sharing options...
dsonesuk Posted February 10, 2013 Share Posted February 10, 2013 (edited) <?php $sev = array_filter($_POST['sev']);$status = array_filter($_POST['status']);foreach ($sev as $sevn){echo 'Sev value individually: '.$sevn.'<br />';} foreach ($status as $stat){echo 'Status: individually: '.$stat.'<br />';} echo 'Sev Grouped together: '.implode(', ',$sev).'<br />';echo 'Status Grouped together: '.implode(', ', $status).'<br />';?> All depend how, or what you want to save, individually or grouped together, want to include null/empty value, or not (which is what array_filter() does remove null and false values), depends if you have set it up so they must fill all addition inputs before processing, don't know? let you figure that out. Edited February 10, 2013 by dsonesuk Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 10, 2013 Author Share Posted February 10, 2013 Thanks for the quick response dsonesuk I tried the above code and its giving me a warning(undefined index sev) upon load the page for the first time, but when i submitting the page its print the values coz sev got the value from variable. could you please explain me how could i use this code with two files, index.php and validation.php, Please read my earlier post i had paste the code, And also please explain how could i use the mutiple value in my validation.php which come from index,php Please let me know if you dont understand Link to comment Share on other sites More sharing options...
dsonesuk Posted February 10, 2013 Share Posted February 10, 2013 That is because you placed it in index.php, its more for the validation.php than index page. if you plan to insert values into the database from each multiple fieldset of form inputs one at a time use the first, the id field must be able to be duplicated if you wish to use same id.if you plan to insert values into the database from each multiple fieldset of form inputs in one row, with the values comma separated, use the second the id can be unique with this, i can see difficulty with textarea content doing it this way though. Link to comment Share on other sites More sharing options...
Ashish Sood Posted February 11, 2013 Author Share Posted February 11, 2013 Yes you are correct, i dont know how use it in my validation.php file sir.please help me to solve this issues ... Link to comment Share on other sites More sharing options...
dsonesuk Posted February 11, 2013 Share Posted February 11, 2013 I don't know what you are looking for, if you enabled additional multiple identical named form elements, you must have considered how you wanted to add all these additional input data to the database, before even considering it. One way would be to store all these additional input into a 'many to one' table, where the id ref of many, would link to the id ref of 'one to many' of the user. name, address etc. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now