How to check which button have been submitted - php

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.

Related

Radio Buttons / Checkboxes

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.

PHP newbie, how to send data on new page?

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.

Submit form without input field.

I am trying to submit a form (or a request) to do some tasks. The form doesn't have input field except a submit button.
I know it's kinda strange but I want to submit the form and get the data by using only php.
Is that possible?
My codes
if($_GET['submitImage']){
//do the stff I want......
}
<form id='formSubmitImage' name='submitImage' method='get' action='cm_ct_generate_preview.html'>
<?php
echo "<table>";
echo "<th>ID</th>";
echo "<th>Type</th>";
echo "<th>Name</th>";
echo "<th>Image</th>";
foreach($tests as $test){
echo '<tr>';
echo '<td>';
echo $test['ID'];
echo '</td>';
echo '<td>';
echo $test['Type'];
echo '</td>';
echo '<td>';
echo $test['Name'];
echo '</td>';
echo '<td>';
echo $test['FileName'];
echo '</td>';
echo '</tr>';
}
echo "</table>";
echo "<input type='submit' value='Set Images'></input><br>";
?>
</form>
Are there any alternative way to do this? Thanks a lot!
Instead of:
echo "<input type='submit' value='Set Images'></input><br>";
Why not:
echo "<button type='submit'>Set Images</button><br>";
You can add a GET parameter to the end of your action URL (in your case it would then be something like cm_ct_generate_preview.html?submitImage=1 or you can add a hidden input field in the form and check if that is submitted, for example:
<input type="hidden" name="submitImage" value="1" />
As your form method is already set to GET it should be sent as a GET parameter.
If the user needs to activate it, let them just press the submit button. However if you want to do it automatically, you will need to use a little javascript. To send the data with it, you can just specify your inputs like this:
echo '<input type="hidden" name="id" value="'.$test['ID'].'">';
the input fields will not appear but the data will be send like with an input field.

identify jquery button click in php rows

I have an input button in :
while ($row = $result->fetch()) {
echo '<table class="cart-row" cellspacing="0" cellpadding="0" width="100%">';
echo '<tbody>';
echo '<tr>';
echo '<td width="75"><img border="0" width="59px" height="78px" title="" alt="" src=' . $row["ImagePath"] .'></td>';
echo '<td width="203"><span id="itemName" class="itemElements">'. $row["Name"] .'</span></td>';
echo '<td width="135"><span id="qtyNum">('. $row["Qty"] .')</span> <br />';
echo '<span id="qtyRemoveLink"><input class="linkbtn" type="submit" id="btnRemove" value="Remove"></td>';
echo '<td width="180"><span id="orderStatus" class="itemElements">In Stock Usually dispatched within 24 hours</span></td>';
echo '<td width="175" id="itemPriceRow"><span id="itemPrice">€ '. $row["Price"] .'</span></td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '<br>';
}
I'm trying to use this method to trigger an event when the button is clicked however the event is only being fired for the first button generated in the first row. I'm using this method:
$(document).ready(function() {
$('#btnRemove').click(function() {
alert("test");
});
});
Any ideas how I can fix this problem? I know in C# there is on row databound method however in jQuery I dont now if it exsists. Thanks
You are selecting an element to bind the click event handler to that has a unique ID. When you select by ID you will be returned only the first matching element because to be a valid ID it must be unique.
Change the ID to a class and change your selector to .btnRemove and the alert will work for all the buttons.
echo '<span id="qtyRemoveLink"><input class="linkbtn" type="submit" id="btnRemove" value="Remove"></td>';
Should change to:
echo '<span id="qtyRemoveLink"><input class="linkbtn btnRemove" type="submit" value="Remove"></td>';
And:
$('#btnRemove').click(function() {
Should change to:
$('.btnRemove').click(function() {
Here is a demo: http://jsfiddle.net/rSyCw/
Here is an excellent answer on the site regarding the creation of valid HTML IDs: What are valid values for the id attribute in HTML?
Use a class for the button, not an ID. IDs are unique identifiers (can only be one with that name on the whole page), however classes may be applied to multiple elements. So...
<input type="submit" id="btnRemove" />
Becomes
<input type="submit" class="btnRemove" />
And, as such, your selector changes to .btnRemove.
And, now that you know that IDs need to be unique, you should go ahead and re-configure the output to use classes, or use IDs with a unique ID appended. e.g.
<?php
while ($row = $result->fetch()){
?>
<!-- Other HTML -->
<input type="submit" id="btnSubmit_<?= $row['Unique_Column_Id']; ?>" ... />
Then you can modify your selector:
$('[id^="btnSubmit_"]').click(...); // All elements with an
// ID that starts with "btnSubmit_"
One red flag here is that you are generating multiple elements with the same ID on each iteration of your loop. Element IDs need to be unique in your document. My guess is that jQuery is only binding to the first element it finds with the ID "btnRemove" because it assumes that your IDs are unique.
As others have stated, using a class name on your button would help your problem, but you should still come up with a way to put unique IDs on your itemName, qtyNum, qtyRemoveLink, orderStatus, itemPriceRow, and itemPrice elements, and anywhere else you might be using this technique.

Assign every TD in PHP While Loop Table a Link

I have a while loop that retrieves info from the mysql db. Now there is a column called profile. So I want every <td> in the profile to be a button that would have an action which would lead to a PHP page. How do I do that?
<form action="profile.php" method="get">
while($result)
{
echo '<table>';
echo '<tr>';
echo '<td>';
echo $result['profile'];
echo '</tr>';
echo '</td>';
echo '</table>';
}
Now every $result['profile'] should be a submit like - <input type="submit">
Thanks.
rewrite
echo $result['profile'];
to this code:
echo ''.$result['name'].'' ;
First of all,
echo '</tr>';
echo '</td>';
should be:
echo '</td>';
echo '</tr>';
To add a Submit button, change
echo '<td>';
to
echo '<td><input type="Submit" value="Submit">';
and
echo '</td>';
to
echo '</input></td>';
You will need to add the appropriate attributes to the submit button if applicable. Also, you will need to surround the submit button with a form element. Whether each link has its own form, or the entire table is surrounded by one is up to you.
Edit
To surround the table with a form tag, change echo '<table>'; to echo '<form><table>';. Then, change echo '</table>'; to echo '</table></form>';. Submit buttons are automatically "linked" to the form element they are contained in. All you have to do is to define the form action.
Edit
Actually, because you are using a GET request, this entire thing can be simplified by using links instead of forms. So go with RAMe0's answer.

Categories