I wasn't sure how to word the title, because just thinking about this concept kind of confuses me and I'm not sure why. I'm sure there is some simple solution to this.
Here is a picture of my codes (Attached below) output.
The user should be able to click 1 radiobox inside each box, but I can't seem to make it do that. The way it is right now, only 1 vacation button can be selected and only 1 absent button can be selected.
Here is my code:
if($loop < 4) {
echo '<td>';
echo ' NAME<BR><input type="radio" name="vacationTeammates[]" value="'.$value[0].'">Vacation<br>
<input type="radio" name="absentTeammates[]" value="'.$value[0].'">Absent';
echo '</td>';
$loop++;
} else {
$loop = 1;
echo "</tr><tr align='center'>";
echo '<td>';
echo ' NAME<BR><input type="radio" name="vacationTeammates[]" value="'.$value[0].'">Vacation<br>
<input type="radio" name="absentTeammates[]" value="'.$value[0].'">Absent';
echo '</td>';
}
(The loop makes it so that the table row ends and starts a new one, so it is somewhat irrelevant but the radio buttons are what matters)
Each radio button pair must have the same "name" attribute value for browsers to know they're their own set.
Change your code to something like this, where "$SomeUniqueRowVar" is a variable that changes for each row:
echo 'NAME<BR>';
echo '<input type="radio" name="status'.$SomeUniqueRowVar.'" value="vacation">';
echo 'Vacation<br>';
echo '<input type="radio" name="status'.$SomeUniqueRowVar.'" value="absent">';
echo 'Absent';
I would add a counter and assign each name to a specific array element, rather than just []. Since you only want one of the radios to be selectable for each teammate, the inputs will have to be the same name but have different values. Put the teammates name in an array (a variable separate from the radio inputs) that uses the counter as it's index. This way you know which response goes to which teammate.
if($loop < 4) {
$name[$counter] = $value[0];
echo '<td>';
echo ' NAME<BR><input type="radio" name="teammates[$counter]" value=1>Vacation<br>
<input type="radio" name="teammates[$counter]" value=0>Absent';
echo '</td>';
$counter++;
$loop++;
} else {
$name[$counter] = $value[0];
$loop = 1;
echo "</tr><tr align='center'>";
echo '<td>';
echo ' NAME<BR><input type="radio" name="teammates[$counter]" value=1>Vacation<br>
<input type="radio" name="teammates[$counter]" value=0>Absent';
echo '</td>';
$counter++;
}
Now when you process the array, you will know if teammates[$counter] is 1 it means vacation and 0 it means absent.
You could also opt to use the name AS the index if you can be sure it is free of special characters/safely parse special chars out.
Thank you #user3163495 & #FirstOne for a few tips I used in my answer here.
I made both of the radio buttons in each cell have the same name, and all the others have different names.
if($loop < 4) {
echo '<td>';
echo ' <input type="radio" name="goneTeammates['.$uniqueLoop.']" value="'.$value[0].'|V">Vacation<br>
<input type="radio" name="goneTeammates['.$uniqueLoop.']" value="'.$value[0].'|A">Absent<br>NAME';
echo '</td>';
$loop++;
} else {
$loop = 1;
echo "</tr><tr align='center'>";
echo '<td>';
echo ' <input type="radio" name="goneTeammates['.$uniqueLoop.']" value="'.$value[0].'|V">Vacation<br>
<input type="radio" name="goneTeammates['.$uniqueLoop.']" value="'.$value[0].'|A">Absent<br>NAME';
echo '</td>';
}
For the value, I used the teammates name (Actually their uniqueid) and an A for absent or V for vacation. The output would basically look like:
35|A
23|V
64|A
etc
On the processing side I explode()'d the input at that | sign, to basically get the uniqueID and the letter in seperate variables, and processed like normal from there.
Related
The form/table filled with data based on the the status. So the rows in the table are build dynamically.
What i want is that the person who wants to submit the form, has filled in the three fields (STIN / STOUT / REASON) (are dropdown menu's) and the field ITEM must be filled in from the database (not a user input).
If it was one row i think it would be easy, but it depends on the status of an order how many rows it will display and depends on the field ITEM if it has an ITEM.
Example:
Row 1 has the item STIN is selected STOUT is selected REASON is
selected
Row 2 has no item STIN is selected STOUT is selected REASON is
selected
Row two or row 3 with no item must be checked and all the other available rows must be checked when the generate xml button is clicked.
<?php
while ($row = sqlsrv_fetch_array($result)) {
$S1 = $row['Qty_ExchangeStock'];
$S2 = $row['Qty_InService'];
$S3 = $row['Qty_TotalStock'];
$status = '1';
$Stock = $S1 - $S2;
if ($Stock < 1) {
$status = '2';
$Stock = 0;
}
echo '<tr>';
echo '<td><input type="text" name="SONR-'.$counter.'" value="'.$row['No_'].'" size="6" readonly /></td>';
echo '<td><div>'.$row['Brand']. '</div></td>';
echo '<td><div>'.$row['Model']. '</div>';
echo ' <input type="hidden" name="SIGC-'.$counter.'" value="'.$row['Service Item Group Code']. '" /></td>';
echo '<td><input type="text" name="ITEM-'.$counter.'" value="'.$row['Item No_'].'" size="8" readonly /></td>';
echo '<td class="center"><div>'.$Stock. '</div></td>';
echo '<td class="center"><div>'.$row['Claim']. '</div></td>';
echo '<td><input type="text" class="small" maxlength="20" placeholder="Serienummer" name="SNR-'.$counter.'" /></td>';
echo '<td><input type="text" class="small" maxlength="10" placeholder="Approval NR" name="APPNR-'.$counter.'" /></td>';
echo '<td><select id="check" name="STIN-'.$counter.'" class="small">'.$ruilin.'</select></td>';
echo '<td><select name="STOUT-'.$counter.'" class="small">'.$ruiluit.'</select></td>';
echo '<td><select name="REASON-'.$counter.'" class="small">'.$dropdown.'</select></td>';
echo '<td><input type="text" maxlength="70" title="Opmerking: maximaal 80 tekens" name="OPM-'.$counter.'" /></td>';
echo "</tr>\r\n";
$counter++;
}
?>
</tbody>
</table>
<input type="submit" value="Generate XML">
</form>
</div>
</div>
<?php
}
?>
You probably want to start off with server-side validation. The reason is people can turn off validation or use scripting to make calls and you need to be able to validate the logic properly on the server.
In this case you need to define a server-side API (what data does the server have to know?), check and document it. But that starts with design and I don't think you are there yet.
Once you have that done you can add similar checks in Javascript. This improves user experience but it is no replacement for server-side checks.
Hello I think I am really close but I just cant seem to get my statement working correctly
I have a overtime field from Mysql and I can display it but I am trying to make it either checked or not checked depending on if it is a 1 for checked or 0 for not checked. I can show the 1 or the 0 onto a Text field but the checkbox just won't work for me.
echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]" <? if($list_hours["overtime"]==1){ echo "checked=checked"} ?> >';
My complete code:
<?
// This next section is to load the text fields from the mysql
// this is to collect all of the logged hours on the job sheet and then display
// the hours already entered.
$hours_query = "SELECT * FROM hours where job_number = '$job_number'";
$hours_result= ($mysqli-> query($hours_query));
// this next section collects the data from the parts table and then adds it to an array
// this then goes into a while statement until it has displayed all of the results it has
// depending on how many parts were used
while ($list_hours = mysqli_fetch_array($hours_result))
{
echo " <table border ='0' table width = 70%>";
echo '<td> <input type="hidden" name="chg_hrs_id[]" value="'.$list_hours['unique_id'].'">';
echo '<td> Start Time <input type="time" name="chg_hrs_str[]" value="'.$list_hours['start_time'].'" >';
echo '<td> Finish Time <input type="time" name="chg_hrs_fin[]" value="'.$list_hours['finish_time'].'">';
echo '<td> Date Of Work <input type="date" name="chg_hrs_date[]" value="'.$list_hours['date'].'">';
echo '<td> Ovt Boolean <input type="ovt_Booloean" name="chg_hrs_ovtb[]" value="'.$list_hours['overtime'].'">';
echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]" <? if($list_hours["overtime"]==1){ echo "checked=checked"} ?> >';
echo "</tr>";
}
echo "</table>";
?>
I think you have some Syntax issues. Try the following:
echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]"' . ($list_hours["overtime"]==1 ? 'checked="checked"' : '') . '>';
Thanks to Marc I had got the syntax incorrect I have used Marc's syntax and it has worked straight away. I now have to see how the statement is put together.
It seems like the dots is a link to connect statements together and it doesn't seem to actually need a if statement to be setup.
I have a form, like this:
echo '<table align=center border=1>';
echo '<form method="post">';
echo '<tr>';
echo '<th>Lista Echipamente</th>';
echo '<th>Actiune</th>';
echo '</tr>';
while($row=mysql_fetch_array($sql)){
echo '<tr>';
echo '<td><input class="search-logi" id="tip" type="text" name="tip" value="'.$row['tip'].'"></td>';
echo '<td><input type=submit name=modifica value=Modifica></td>';
echo '</tr>';
}
echo '</form>';
echo '</table>';
Because of the loop, this form will have multiple rows. For each input, will be a button.
Lets say I want to add a value in the second input, then I will submit it (also the second button, obviously). Here comes the problem, in my code I have just a button, but the user see as many as the loop goes. How can I check which button is submitted?
Alternatively, you could utilize <button> tags for this purpose:
echo '<form method="post">'; // form tags are outside to be valid!!!
echo '<table align=center border=1>';
echo '<tr>';
echo '<th>Lista Echipamente</th>';
echo '<th>Actiune</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($sql)){
echo '<tr>';
echo '<td><input class="search-logi" type="text" name="tip['.$row['id'].']" value="'.$row['tip'].'"></td>';
echo '<td><button type="submit" name="modifica" value="'.$row['id'].'">Modifica</button</td>';
echo '</tr>';
}
echo '</table>';
echo '</form>';
Then on the processing form:
$modifica = $_POST['modifica']; // shoud return the index num
$tip = $_POST['tip'][$modifica]; // select which textbox
Keep some unique reference to both input and button. Let say start a counter as 1 and then make the input id as input_1 and button as button_1 and then when you click button get the counter value that is 1 for above and then get the value of input_1 as per counter 1 and submit the form. And then increase counter so that id will increase as well. I think this will help you what you need.
Check all your $_POST variales which are empty and which are not.
Else you could write a javascript function to set all unused fields the value to "0" or to a string and check if it is used or not e.g "unused". In your php script you can check now for the value if it is set to "0" or "unused".
One way is to prefix the name of the input text field with a counter or other prefix. Bases on the name of the input field you can determine which button fired.
I have a quiz test page where I want to test the user for knowledge.
I did it so it works, but it has a flaw.
This is my code for populating the quiz with questions and (possible) answers from a mysql database:
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="post" id="quiz">';
$sql=mysql_query("SELECT idtest,no_q,title_q,q,idteste_dtl,corect_a from teste_dtl where idtest=".$idtest." order by no_q");
$num = mysql_num_rows($sql);
$i=0;
while ($row=mysql_fetch_row($sql)) {
echo '<h3 style="color:#e74c3c">'.$row[2].'</h3>';
echo '<p data-toggle="tooltip"><strong>'.$row[3].'</strong></p>';
$ssql=mysql_query("select letter,answer from tests_answers where idtest=".$idtest." and idq=".$row[4]." order by letter");
while ($rrow=mysql_fetch_row($ssql)) {
$Label = 'question-'.$row[1].'-answers-'.$rrow[0];
echo '<div>';
echo '<label class="radio">';
echo '<input type="radio" data-toggle="radio" name="answers['.$row[1].']" id="'.$Label.'" value="'.$rrow[0].'" unchecked>';
echo $rrow[0].') '.$rrow[1];
echo '</label>';
echo '</div>';
}
echo ' <br>';
echo '<input type="hidden" name="question['.$row[1].']" value="'.$row[2].'">';
echo '<input type="hidden" name="corect_a['.$row[1].']" value="'.$row[5].'">';
echo '<input type="hidden" name="idemp" value="'.$idemp.'">';
echo '<input type="hidden" name="idtest" value="'.$idtest.'">';
echo '<input type="hidden" name="idfirm" value="'.$idfirm.'">';
echo '<input type="hidden" name="namefirm" value="'.$namefirm.'">';
echo '<input type="hidden" name="totalq" value="'.$num.'">';
}
echo' <input type="submit" class="btn btn-hg btn-primary" value="Check answers" />';
echo '</form>';
So basically I generate a pair of text (question) + radioboxes (possible answers) from 2 tables. Table 1 is called "teste_dtl" and Table 2 is called "tests_answers".
All works perfectly except there is a bug. If the user does not check a radio box in a question... my code seems to think that the first radio box was checked.
So I have something like:
Question ONE
Body of question one... some text... and so on
A) first answer
B) second answer
C) third answer
D) fourth answer
Question TWO
Body of question two... some text... and so on
A) first answer
B) second answer
C) third answer
D) fourth answer
...
So the A,B,C,D... are checkboxes (input type radio). They are all unchecked from the start.
When I POST the results... apparently the questions that were not answered (no radio was checked) are considered as "answered with answer A)"
How can I avoid this?
Thank you
Radio buttons are supposed to always have a value.
The HTML specification says: "If no radio button in a set sharing the same control name is initially 'on', user agent behavior for choosing which control is initially 'on' is undefined." This means, practically, that depending on the browser and/or the Javascript library used to submit the form (if you're using AJAX), you can get unpredictable results.
You should force one of the buttons to be selected either by inserting checked="checked" like this:
$first_answer = true; // Set a flag for the first answer
while ($rrow=mysql_fetch_row($ssql)) {
$Label = 'question-'.$row[1].'-answers-'.$rrow[0];
echo '<div>';
echo '<label class="radio">';
// Output the beginning of the INPUT tag
echo '<input type="radio" data-toggle="radio" name="answers['.$row[1].']" id="'.$Label.'" value="'.$rrow[0].'"';
if ( $first_answer ) { // When the flag is true...
echo ' checked="checked"'; // ...add the 'checked' parameter...
$first_answer = false; // ...and set the flag to false for the next round
}
echo '/>'; // Close the INPUT tag
echo $rrow[0].') '.$rrow[1];
echo '</label>';
echo '</div>';
}
Another option is to add some Javascript to check that the user has selected an answer.
I'm learning PHP and I need help to "send" informations on an another page.
I read a text file and I put data on 2 arrays :
$nomVille;
$nomFichier;
$index; // index is use for arrays
Now I want to build a drop-down list and a submit button.
The drop-down list : show all items on $nomVille
The button : open "villes.php"
villes.php will have to retrieve $nomFichier and $index but I'm not able to do that.
Here is a part of my code :
echo 'Make your choice : ';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<FORM METHOD="post" ACTION="villes.php">';
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
Is that correct ?
You've not got your select element wrapped in form tags which will prevent it being sent. What you actually want is this:
echo '<FORM METHOD="post" ACTION="villes.php">';
echo 'Make your choice : ';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
On the page "villes.php", you should then be able to refer to the variable $_POST["ville"] which will contain the selected option in the select.
echo $_POST["ville"];
In order to transfer the values of $nomFichier and $index, you want to send them as hidden elements as part of the form, so add the lines:
echo "<input type='hidden' name='nomFichier' value='".$nomFichier."'>";
echo "<input type='hidden' name='index' value='".$index."'>";
Which will again, be able to be grabbed from the $_POST array.
Moving your select into the FORM element will allow the application to post the data on submit.
echo 'Make your choice : ';
echo '<FORM METHOD="post" ACTION="villes.php">';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
From the villes.php page you then have data retrievable in the data that you can access like this:
if (isset($_POST['ville'])) {
$someVar = $_POST['ville'];
}
You'll find it worth your time to Google "PHP form tutorial" and seeing how things work from there.