I have a PHP form which shows one 'User details' block by default. 'User details' section has first name, last name and address etc. I have a requirement to repeat the same block after clicking on the button so the user can enter 'n' number of users into on the form. Also I need to save all the information in MySQL database. What is the best way to achieve this? Please let me know. I appreciate any help.
Thank you.
I would use jQuery to add additional form elements. You'll need to give each element a different 'name' attribute though- I'd use something like name='user1name' (and in the next username field name='user2name'). Comment on my post if you need specific help with that. When the form submits, I'd use php to loop through each set of fields with an insert statement at the end of the loop.
PHP is something like this:
for ($i = 1; is_null($_POST['name' . $i]) == false; $i++) {
$name = $_POST['user' . $i . 'name'];
// repeat for all your fields
// do your insert statement here - 1 insert per user created
mysql_query("INSERT INTO table_name (Name, Address, ...) VALUES ('$name', '$address', ...)");
}
jQuery is something like this:
Existing HTML
Should only have 1 div with class=user at this point
<div id='fields'>
<div class='user'>
<input type='text' name='user1name' />
<input type='text' name='user2name' />
... more elements ...
</div>
</div>
jQuery
var userFields = $("div.user").clone();
$(userFields).appendTo("div#fields");
$("div#fields").children("div.user:last").children().each(function() {
// change names here!
});
This will copy the existing field and add its copy to the fields div. It will also set the new names for the second set of user fields. Note: you only need ot set userFields once, and then you can use $(userFields).appendTo("div#fields"); as many times as you need to (as long as the userFields variable is in scope).
If you have event handlers attached to your elements in the user field, use $("div.user").clone(true, true); instead of $("div.user").clone();.
Related
I would like to know if it is possible to help me please.
It's a live ajax search that retrieves information in the database. This is the php code :
<?php
$key=$_GET['key'];
$array = array();
$connection=mysqli_connect("localhost","root","","visitor_signin_app");
$query = mysqli_query($connection, "SELECT * FROM visitors WHERE visitor_first_name LIKE '%{$key}%' AND visitor_visit_status = 'Signed Out'");
while($row = mysqli_fetch_assoc($query)) {
$array[] = '<span style="display:none; visibility:hidden">'.$row['visitor_id'].'</span>' . ' ' . $row['visitor_first_name'] . ' ' . $row['visitor_last_name'];
}
echo json_encode($array);
mysqli_close($connection);
?>
In the array I am trying to hide the 'visitor_id' and when I start typing the name in the input field it works perfect where it only shows the first name and last name, but once I select the name that displays in the dropdown then it inserts the
<span style="display:none; visibility:hidden">1</span>
So my main question is, would it be possible to hide the html that I dont want to display in the input field but the value needs to be added with the name that gets chosen. Any advice would be gladly appreciated. Thank you
This is the link where I received the code : https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/
I would like to retrieve all the information from the registered person that gets selected in the dropdown list and add them as a new entry to the database.
Why not use the HTML inputwith type hidden ? See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden
I think it's made exactly for your purpose.
Your question is a bit confusing to understand, but if I'm correct, what you want to do is have a hidden input field, but set the value of the input field based on the value of a different input field.
To do that, you should give your hidden field a display: none style, and use JavaScript to set it's value. Something like this:
document.getElementById('inputId').value = 'your data';
Please see this link
http://thedesigningworld.com/bea
Here's a Small form contains 8-9 fields + a group of checkboxes
I want to save all details in DB + want to display in a table in proper manner, but it not works properly
Here's the code which i used
for($i=0;$i<count($_POST[wert1]);$i++)
{
if($_POST[wert1][$i]!= "")
{
$check1[] =$_POST['wert1'][$i]; } }
$new1=implode(',', $check1);
$result = "INSERT into table1(check1) values($new1)";
$result = mysqli_query($con, $result);
So i've one doubt that for each checkbox row, should i need to define same array name or different like here i used array name as wert1[] for first row
Checkbox values are not transmitted if the box is not checked.
If you have influence, you could put a hidden input field of the same name before the checkbox and the value "0", like:
<input type="hidden" name="checkbox_name" value="0" />
<input type="checkbox" name="checkbox_name" value="1">Some Text</input>
In you example site, you're using array notation, which is basically a good thing. However, you have not given an index so you might not recognize missing elements.
I have a div in my page called .highlights.
In this div I have a unknown numbers of text input(<input type="text" />). It can range from 0 to unknown.
When someone clicks at submit, I want to store in PHP all the values of the inputs, into one variable called myHighlights. The values must be seperated by ;
<input type="text" name="unlimited[]" />
if( isset($_POST['submit_button']) ) {
// Skip blank values
$unlimited = array_filter( $_POST['unlimited'] );
$myHighlights = implode(';', $unlimited);
}
To begin with, you'll have to assign names to the controls so they get sent together with the rest of of the form. Please have a look at the How do I create arrays in a HTML <form>? entry of the PHP FAQ for a nifty trick.
if($_POST)
{
$myHighlights = implode(';',$_POST);
print_r($myHighlights);
}
I'm creating an HTML form, which takes some of its options from a database (php + mysql)
In the php, I'm creating a checkbox input, with a select box next to it.
I named the checkbox houseAppsSelected[] and the select customCategories[], so I'll get the values as an array.
I append all the HTML into a var called $options, and I echo it later on.
while ($row=mysql_fetch_array($result)) {
$cat_id=$row["category_id"];
$cat_name=$row["category_name"];
$options.="<INPUT type=\"checkbox\" name=\"houseAppsSelected[]\" VALUE=\"$cat_id\">".$cat_name." ---> ";
$custom_sql="SELECT custom_cat_id, cat_name FROM custom_categories WHERE house_app='$cat_id'";
$custom_result=mysql_query($custom_sql);
$options.="<SELECT name=\"customCategories[]\">";
$options.="<OPTION value=\"0\"> Choose Category </option>";
while ($custom_row=mysql_fetch_array($custom_result)) {
$custom_id = $custom_row['custom_cat_id'];
$custom_name = $custom_row['cat_name'];
$options.="<OPTION value=\"$custom_id\">".$custom_name."</option>";
}
$options.="</SELECT> <br /> <br />";
}
I want to have the checkbox control whether the select box is enabled or disabled.
I found this article, which makes it look easy, but if all the select boxes have the same name, it will disable all of them.
Is there a way to have a specific checkbox disable/enable only a specific select box, if I build them dynamically with php? (they all have the same name).
You can use the nextSibling property to find the select.
function chkboxClick(chkbox) {
chkbox.nextSibling.nextSibling.disabled = !chkbox.checked;
}
Add the click handler like this:
<INPUT type="checkbox" onclick="chkboxClick(this)" ... />
Demo here: http://jsfiddle.net/gilly3/vAK7N/
You can give each tag a unique "id" value, which is independent of the "name". Then you can use
var elem = document.getElementById(something);
to access them by that unique value.
Exactly how your php code makes up the unique values sort-of depends on what you need, exactly. It can really be anything.
I've just started using jQuery. One thing I've been using it for is adding rows to a table that is part of a form.
When I add a new row, I give all the form elements names like 'name_' + rowNumber. I increment rowNumber each time I add a row.
I also usually have a Remove Row Button. Even when a row is removed, the rowNumber count stays the same to keep from repeating element names.
When the form is submitted, I set a hidden element to equal the rowNumber value from jQuery. Then in PHP, I count from 1 to the rowNumber value. Then for each value, I perform an isset($_REQUEST['name'_ . index]). This is how I extract the form elements that remained after deleting rows in jQuery.
Does anyone here have a better technique for accounting for deleted rows?
For some of our simpler tables, we use a field name such as 'name[]', though for JavaScript they would need a usable id.
It does add some complexity in that 'name[0]' has to assume 'detail[0]' is the correct element.
PHP will create an array and append elements if the field name ends with [] similar to
<input name="field[]" value="first value" />
<input name="field[]" value="second value" />
// is roughly the same as
$_POST['field'][] = 'first value';
$_POST['field'][] = 'second value';
Use arrays to hold you values in your submission. So bin the row count at the client side, and name your new elements like name[]. This means that $_POST['name'] will be an array.
That way at the server side you can easily get the row count (if you need it) with:
$rowcount = count($_POST['name']);
...and you can loop through the rows at the server side like this:
for ($i = 0; isset($_POST['name'][$i]; $i++) {}
You could extract all the rows by doing a foreach($_POST as $key => $value).
When adding a dynamic form element use the array naming method. for example
<input type="text" name="textfield[]" />
When the form is posted the textfield[] will be a PHP array, you can use it easily then.
When you remove an element make sure its removed from the HTML DOM.
Like blejzz suggests, I think if you use $_GET, then you can just cycle through all of the inputs that were sent, ignoring the deleted rows.
foreach ($_GET as $k=>$v) {
echo "KEY: ".$k."; VALUE: ".$v."<BR>";
}
I notice that you mention "accounting for deleted rows"; you could include a hidden input, and add a unique value to it each time someone deletes a row. For example, the input could hold comma-separated values of the row numbers:
<input type="hidden" value="3,5,8" id="deletions" />
and include in your jQuery script:
$('.delete').click(function(){
var num = //whatever your method for getting the row number
var v = $('#deletions').val();
v = v.split(',');
v.push(num);
v = v.join(',');
$('#deletions').val(v);
});
Then you should be able to know which rows were deleted (if that is what you were looking for).
you can use POST or GET
After submit you can use all of your form element with this automaticly. You dont need to reorganise your form element names. Even you dont need to know form elements names.
<form method="POST" id="fr" name="fr">.....</form>
<?php
if(isset($_POST['fr'])){
foreach($_POST as $data){
echo $data;
}
}
?>
Also you should look this
grafanimasyon.blogspot.com.tr/2015/02/veritabanndan-php-form-olusturucu.html
This is a automated form creator calcutating your database tables. You can see how to give name to form elements and use them.