I have a javascript which creates two types of elements.
The PHP code is added on submiting the form, but I show it above.
The First type looks like this:
<tr class="disc">
<span><?php echo $disc_number ?></span>.
</tr>
The Second type looks like this:
<tr class="track">
<input name="trackNumbers[]" value="<?php echo $track_number ?>" />
<input name="discNumbers[]" value="<?php echo $disc_number ?>" />
</tr>
The "track" elements can be multiple for one "disc" element.
For example:
<tr class="disc">
<span>1</span>.
</tr>
<tr class="track">
<input name="trackNumbers[]" value="1" />
<input name="discNumbers[]" value="1" />
</tr>
<tr class="track">
<input name="trackNumbers[]" value="2" />
<input name="discNumbers[]" value="1" />
</tr>
<tr class="disc">
<span>2</span>.
</tr>
<tr class="track">
<input name="trackNumbers[]" value="1" />
<input name="discNumbers[]" value="2" />
</tr>
<tr class="track">
<input name="trackNumbers[]" value="2" />
<input name="discNumbers[]" value="2" />
</tr>
To show on submit each "track" element I am using "for" command.
for ($track_i = 0; $track_i < $total_tracks; $track_i++)
But what about "disc" elements? How to make my "disc" elements to also display on submit, and to have their "track" elements after them.
If I catch your meaning correctly, you need to associate the track elements with the disc numbers... so use a multi-dimensional array:
<tr class="track">
<input name="trackNumbers[<?php echo $disc_number ?>][]" value="<?php echo $track_number ?>" />
</tr>
If I recall correctly, this will produce (in PHP when submitted) an array of arrays, where each first-level array (one per disc) contains an array of tracks (one per track).
foreach($_POST['trackNumbers'] as $discNumber => $tracksArray) {
...
The way you have it setup now is you have two separate arrays track and disc, to get the track number associated to the proper disc I would set up the input element like so:
<input name="trackNumbers[1][]" value="2">
Where 1 is the disc number, you may want to pass in disc numbers once and then use them on the backend to loop through and pull out all the tracks that correspond to the disc numbers.
Related
Here i am facing a problem with the while loop.. (i think) unable to store my all retrieve the data from Mysql table to my html form.
the first row is only getting posted and the rest of the rows are not posting due to same name element getting repeated from the while loop,
Here the $_POST['mechanic_name']; one time i'm using.. any problem with this..
because this is not in a while loop, ore if you think any other problems with the code below pls advice
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
</tr>
<!--* For save php script*-->
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,) VALUE ('$mechanic_ID','$mechanic_name'[enter image description here][1])";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
plz help me...!`
<div>
<form role="form" method="post" action="schedule.php">
<table class="table table table-inverse table-hover">
<fieldset>
<div>
<input class="form-control" placeholder="Username" name="month" type="Month" value="January">
</div>
<thead class="thead-inverse">
<tr>
<th>Mechanic Id</th>
<th>Mechanic Name</th>
<th>Woking Day Selection</th>
<th>Delete User</th>
</tr>
</thead>
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
<td>
<div class="weekDays-selector">
<input type="checkbox" name="Sun" id="weekday-sun" class="weekday" />
<label for="weekday-sun">S</label>
<input type="checkbox" name="Mon" id="weekday-mon" class="weekday" />
<label for="weekday-mon">M</label>
<input type="checkbox" name="Tue" id="weekday-tue" class="weekday" />
<label for="weekday-tue">T</label>
<input type="checkbox" name="Wed" id="weekday-wed" class="weekday" />
<label for="weekday-wed">W</label>
<input type="checkbox" name="Thu" id="weekday-thu" class="weekday" />
<label for="weekday-thu">T</label>
<input type="checkbox" name="Fri" id="weekday-fri" class="weekday" />
<label for="weekday-fri">F</label>
<input type="checkbox" name="Sat" id="weekday-sat" class="weekday" />
<label for="weekday-sat">S</label>
</div>
</td>
<td>
<!--button-->
<input class="btn btn-lg btn-success btn-block" type="submit" value="register" name="register" >
</td>
</tr>
</fieldset>
<?php } ?>
</table>
</form>
</div>
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
$Sun=$_POST['Sun'];//same
$Mon=$_POST['Mon'];//same
$Tue=$_POST['Tue'];//same
$Wed=$_POST['Wed'];//same
$Thu=$_POST['Thu'];//same
$Fri=$_POST['Fri'];//same
$Sat=$_POST['Sat'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,month,Sun,Mon,Tue,Wed,Thu,Fri,Sat) VALUE ('$mechanic_ID','$mechanic_name','$month','$Sun','$Mon','$Tue','$Wed','$Thu','$Fri','$Sat')";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
?>
ok
your text input have same static name
you shuld give it an array name if you want to save it like :
<input type="text" name="mechanic_ID[]" value="first_one" />
<input type="text" name="mechanic_ID[]" value="second_one" />
and when get data
$mechanic_ids = $_POST['mechanic_ID'];
// this give you data in array ('first_one','second_one')
can resume
foreach($mechanic_ids as $mechanic_id){
// do some thing in this one
}
here good example about arrays form html and php
here is the things is mechanic table whole data (mechsanic_ID , Mechanic_name) able to retrieve, it's showing all the data; because of while loop, But when register it the very first row only able to posted and the rest of the rows are not postinginterface look like this.
lets say we have this:
echo '<form method="post">
<div class="form-group">
<table class="table table-bordered table-hover table-striped" style="width:auto">
<tr>
<td><label for="array">ARRAY_NAME</label></td>
<td>
<input type="checkbox" name="array[]" value="1" /> option1<br />
<input type="checkbox" name="array[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array2">ARRAY_NAME2</label></td>
<td>
<input type="checkbox" name="array2[]" value="1" /> option1<br />
<input type="checkbox" name="array2[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array3">ARRAY_NAME3</label></td>
<td>
<input type="checkbox" name="array3[]" value="1" /> option1<br />
<input type="checkbox" name="array3[]" value="2" /> option2
</td>
</tr>
</table>
</div>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>';
I tried to implement this code: <?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>
but it didn't work! It only works if you have name="array1" and name="array2". this way I'm thinking I can save multiple data in a parent array saved! Like this form[array1[],array2[],array3[]].
Can someone give me a solution because I'm stuck! Thanks in advance guys!!
You are trying to access the values incorrectly.
You can access a array posted like this:
echo $_POST['array1'][0];
php.net/$_POST See the first comment.
When you put square brackets in the name of form control, PHP will inflate that set of elements with similar names into an array.
You need to access:
$_POST['array1'][0]
not
$_POST['array1[0]'] // This is incorrect
(You also need to match the actual name of your control: Your HTML has array, array2 and array3 but not array1)
what datatypes do i use for checkboxes and radio buttons and for input type=date in the pmySQL??
<pre>
<tr>
<td class="right_align">*Occupation : </td>
<td>
<input type="radio" name="occupation" value="student" />Student<br />
<input type="radio" name="occupation" value="business" />Business<br />
<input type="radio" name="occupation" value="pvtsect" />Priavte Sector<br />
<input type="radio" name="occupation" value="govtsect" />Govt. Sector<br />
<input type="radio" name="occupation" value="others" />Others<br />
</td>
</tr>
<tr>
<td class="right_align">*On which project you want to work :</td>
<td>
<input type="checkbox" name="" ="projectname" value="shine"/>Project Shine<br />
<input type="checkbox" name="" ="projectname" value="sahyog" />Project Sahyog<br />
<input type="checkbox" name="" ="projectname" value="smile" />Spreading Smiles
</td>
</tr>
</pre>
1) you will get string as the value of radio so it will be varchar type
2) for check boxes any one can have multiple values so you need to create a separate table(working_project) for this values( data type will be same as radio) and another table for mapping(user_working_project)
user_working_project table will containing user id and working project id
e.g one user has id=1, and working_project=1,2
then in your user_working_project table entries will be like this:
user_working_project_id working_project_id user_id
1 1 1
2 2 1
I am creating radiobuttons in HTML .I am getting data from SQL Database.I am creating an attendence form in which there will be student name,Present Radio button ,Absent Radio button.
I put the Present and Absent Radio button in a single Group,(so that only one could be clicked.The problem is that I can't create more than one radio group..My code is
<html>
<tr>
<td><?php echo $i;?></td> // Serial Number
<td> <label><?php echo $att['std_name'];?></label></td> //student name
<td><input type="radio" name="std_r[]" id="std_r[]"></td> // for present
<td><input type="radio" name="std_r[]" id="std_c[]"></td> // for absent
<td><input type="hidden" value="<?php echo $att['rollnum'];?>" > </td> //hidden field that will take roll number of student on next page
</tr>
</html>
<?php
I want that for each student there should be seperate group of radio buttons.thats why i want to create buttons with different name.
Anyone who can help me with this
Thanks
I guess what you are asking is there are several students in the same page and each student with his own present/absent option.
You should do something like this:
<input type="radio" name="test[1]" value="present" />
<input type="radio" name="test[1]" value="absent" />
<input type="radio" name="test[2]" value="present" />
<input type="radio" name="test[2]" value="absent" />
<input type="radio" name="test[3]" value="present" />
<input type="radio" name="test[3]" value="absent" />
You may also specific the student_id as index, this will be easier for you do handle in PHP:
<input type="radio" name="test[33888]" value="present" />
<input type="radio" name="test[33888]" value="absent" />
<input type="radio" name="test[90909]" value="present" />
<input type="radio" name="test[90909]" value="absent" />
Can somebody help me? i cant echo my checkbox group with a multiple selection.
Every time i echo the checkbox group the only things show is the last box that Ive check.
here's my code
<?php
$submit = $_POST['submit'];
$incharge = $_POST['incharge'];
if ($submit)
{
echo $incharge;
}
?>
<table width="500" height="69">
<tr>
<td width="73"><label>
<input type="checkbox" name="incharge" value="1" id="responsible_0" />
MNFA</label></td>
<td width="72"><label>
<input type="checkbox" name="incharge" value="2" id="responsible_1" />
HJB</label></td>
<td width="70"><label>
<input type="checkbox" name="incharge" value="3" id="responsible_2" />
JBG</label></td>
<td width="75"><label>
<input type="checkbox" name="incharge" value="4" id="responsible_3" />
MSG</label></td>
<td width="275"><label>
<input type="checkbox" name="incharge" value="5" id="responsible_4" />
MGR</label></td>
</tr>
<tr>
<td height="33"><label>
<input type="checkbox" name="incharge" value="6" id="responsible_5" />
AAP</label></td>
<td><label>
<input type="checkbox" name="incharge" value="7" id="responsible_6" />
EPM</label></td>
<td><label>
<input type="checkbox" name="incharge" value="8" id="responsible_7" />
SGA</label></td>
<td><label>
<input type="checkbox" name="incharge" value="9" id="responsible_8" />
JLL</label></td>
<td><label>
<input type="checkbox" name="incharge" value="10" id="responsible_9" />
AFM</label></td>
</tr>
</table>
Thank you in advance.. .
Change the name attribute to:
name="incharge[]"
That will produce an array, $incharge.
Note that you won't be able to just echo that value; you will need to "print_r" or loop through it.
You need to change the "name" attribute of your "input" elements to indicate that it's an array by adding square brackets [] at the end. $_POST['incharge'] will be an array instead of a string.
Example
<input type="checkbox" name="incharge[]" value="1" id="responsible_0" />
The reason only the last value is being sent is because all the checkboxes have the same name, thereby renaming them over and over. What you want is to assign all the checkboxes to an array, like so:
Change name="incharge" to name="incharge[]"
You'll then want to iterate it:
if ($submit)
{
// PHP throws a fit if we try to loop a non-array
if(is_array($incharge))
{
foreach($incharge as $val)
{
echo $val . '<br />';
}
}
}
if(isset($_post['calculations'])
{
$member = $_POST['member'];//get the total values in an array
if(is_array($member))// confirm $member is an array
{
foreach($member as $names)
{
echo $names ."<br/>";//take the values
}
}
<input type="checkbox" name="member[]" value="value1">