Strange output from variable to hidden form field - php

Below is a function that outputs a form with existing users from a database, with checkboxes and a submit button. The user choose one or several users to be removed and submits the form. What i'm working on right now is to implement a confirmation box in js ("Are you sure you want to remove..."), which shall contain the users that the user wants to remove.
By that reason I'm putting in the usernames in a hidden field that the js function will retrieve the username(s) from. The problem is that not only the usernames are put into the hidden input fields value attribute, but also several input fields as well (!?). If I hard code in for example 'username1,username2' in the attribute it works as it should, but not if I use the variable $users.
public function ShowUsers($userArray) {
$userIdArray = $userArray[0];
$userNameArray = $userArray[1];
$users = implode(",", $userNameArray);
echo $users; // username1,username2...
$nrOfUsers = count($userIdArray);
for ($i = 0; $i < $nrOfUsers; $i++) {
$users .= "<label for='$userIdArray[$i]'>
$userNameArray[$i]
<input type='checkbox' name='$this->_checkBox' value='$userIdArray[$i]' /><br/>
</label>";
}
$userList = "<div class='userList'>
<form id='form3' method='post' action=''>
<fieldset>
<p>Existing users</p>
$users
<input type='hidden' value='$users' />
<input type='submit' id='$this->_submitRemove' name='$this->_submitRemove' Value='Ta bort' />
</fieldset>
</form>
</div>";
return $userList;
}
The output is the following:
<input type='hidden' value='username1,username2<label for='47'>
username1
<input type='checkbox' name='check[]' value='47' /><br/>
</label><label for='50'>
username2
<input type='checkbox' name='check[]' value='50' /><br/>
</label>' />
I just don't get it why this happens? The content of $users (in this case 'username1' and 'username2') is there as expected, but why is the input and label tags put into the value attribute?

The problem is in your for loop. There you're appending all that HTML to the variable $users which already is $users == username1,username2. If you do echo $users; or even var_dump($users); just after you've finished the for loop, you'll see.
What are you trying to achieve in the for loop indeed? Did you rather mean
for ($i = 0; $i < $nrOfUsers; $i++) {
echo "<label for='$userIdArray[$i]'>
$userNameArray[$i]
<input type='checkbox' name='$this->_checkBox' value='$userIdArray[$i]' /><br/>
</label>";
}

Related

Send form when with mysql_fetch_array while loop CLOSED

I have got a while loop that runs through all the records of the database printing them on a table. Now i also have some checkboxes within that very loop that I want to use to submit a form when clicked. Now when I click the checkbox it will indeed submit the form thanks to a Jquery script I found, BUT whenever i submit it it submits with the ID of the first record of the table.This Image
shows the table, as you see the first record has ID 34. Now every checkbox I click will send the $id 34.
This does not happen with normal submit buttons.
Is there a way I can submit with the individual userID's
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
I'm sorry if i'm not very clear with the explanation it is quite hard to explain this situation. Thank you guys!
Probably your problem is that you are submiting the same form always and its because you create a form for each row but it has the same id
For you the easy way is to put each form with the id cointaining the unique value of the row and doing submit with that.
Something like this
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete_<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete_<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
It looks like the <form> your creating has a static id, so ALL forms will have id='resComplete'. The jQuery submit function will grab the first element with id='resComplete' and submit it. You need to make it unique for every form and make the onchange='$("#resComplete").submit();' code match it.
Eg.
<?php
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete-<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete-<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
Better yet, use jQuery to find out what form it's in by chaning the onchange to something like:
<input id='complete' type='checkbox' name='complete' value='1' onchange='$(this).closest('form').submit();' <?php if($complete == 1){echo "checked";}?>>

how to listen event from form in dynamic number of forms php

I have some PHP code that generates and edits forms of users. The number of forms depend on number of users registered. As a developer, I don't know what's the number of users that can register per day.
The code is like this:
for($i=0;$i<$n;$i++)
{
echo "<form method='post'>
<input type='text' name='fname'>
<input type='text' name='lname'>
<input type='submit' name='submit' value='save'></form>";
}
This code can repeat with 4 or 5 or ++ users. When i do:
if(isset($_POST['submit']))
{
//code
}
for recovering the value of the two inputs.
How does the PHP know the source of the event? It can make a mistake because all button has the same name? Please help me!
You can give the button different names then:
for($i=0;$i<$n;$i++)
{
echo "<form method='post'>
<input type='text' name='fname'>
<input type='text' name='lname'>
<input type='submit' name='submit".$i."' value='save'></form>";
}
And then loop through the names to see if (and which) button is pressed:
for($i=0;$i<$n;$i++)
{
if(isset($_POST['submit'.$i]))
{
//code
}
}
If in addition you want to distinguish post values of the inputfields you could index their names alike.

Passing form information in a for-loop

Maybe this is a stupid question, but I was wondering if I could get information passed with a form into a for-loop.
The problem is as follows:
The passing of the information works, though he passes only for the last loop. For example if I click the submit button for loop 2 ($i = 2). The command $_POST['titel'] will only remember the information in last loop ($i = $numact-1) and not loop 2 ($i = 2).
for example if titel[0] = test0, titel[1] = test1, titel[2] = test2. and I click the submit button below titel[0] he passes the information from titel[2]. Is there an easy way to get around this?
I have the following code (For the sake of simplicity I shortened it);
<?php
for ($i = 0; $i <= $numact-1; $i++) {
echo "<tr><td width='150'>
<input type='text' name='titel' value='$titel[$i]' />
</td></tr>
<tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
}
?>
You're using the same parameter name over and over again (name=titel - you had a type and probably meant to write as 'title') so when the form submits - only one value will be passed.
You can easily fix that by passing the parameters as:
...
echo "<tr><td width='150'>
<input type='text' name='titel$i' value='$titel[$i]' />
</td></tr>
<tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
...
and reading it on the other side as title0, title1 etc.
As seen in this post, you can pass in an array simply by putting brackets around the index. PHP will reconstruct the array for you.
Also, you probably want your submit at the end of your table, not after each row. Your code should look something like this:
<?php
for ($i = 0; $i <= $numact-1; $i++) {
echo "<tr><td width='150'>
<input type='text' name='titel['.$i.']' value='$titel[$i]' />
</td></tr><tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
}
?>
The advantage of this approach is that it's more extensible, should you choose to use different values. However, Edson Junior's approach is slightly simpler.
You can change the name attribute to name=titel[] so PHP will automatically transform it to an array. It is not necessary to specify the index inside title[].
For example:
<input type="text" name="titel[]" value="x" />
<input type="text" name="titel[]" value="y" />
<input type="text" name="titel[]" value="z" />
When you submit the form, PHP will transform it to
"title" = array (
0 => "x",
1 => "y",
2 => "z"
);
In case you need to check what button was clicked, you could change the inputs type="submit" to <input type="button" name="submitreg" class="submitreg" id="whatever_you_want_but_has_to_be_unique" />.
Outside the loop, add another input wich will have the value of what button was pressed.
<input type="hidden" id="buttonPressed" name="buttonPressed" value="" />
Then add this code (you need to import jQuery library to the page so it understands the code below, wich is jQuery, here is the Docs.
<script type="text/javascript">
$(function(){
$("button.submitreg").live("click", function(){
var buttonPressed = $(this).attr("id");
("input#buttonPressed").val(buttonPressed);
});
}
</script>
All you need to do is get the buttonPressed value and you'll know what button was pressed.

Submit button in loop

I am having a problem using submit button in loop.
What I am trying to do is to show a list from a table in a database and next to each row a submit button.
That insert into a table the id of that row my code is
for ($i = 0; $i <= count($all_rows)-1; $i++) {
<form>
<input type='text' id='name' value='".$all_tv_shows[$i]['id']."' />
<input type='button' value='Submit' onclick='addRecord()' />
</form>}
I am using AJAX with the submit buttons.
so when i press on any submit button the value that inserted in the table is all the same value (the last row that showed)
Try this, close the php tags properly
<?php for ($i = 0; $i <= count($all_rows)-1; $i++) { ?>
<form>
<input type='text' id='name' value='".$all_tv_shows[$i]['id']."' />
<input type='button' value='Submit' onclick='addRecord()' />
</form>
<php } ?>
You need to close the PHP tags:
<?php
//assuming some other code first...
for ($i = 0; $i < count($all_rows); $i++) {
?>
<form>
<input type='text' id='name' value='<? php echo$all_tv_shows[$i]['id'];?>' />
<input type='button' value='Submit' onclick='addRecord()' />
</form>
<?php
}
Also, it's not hugely important, but it might be easier to read if you change the for loop as above.
I guess the problem is not in the code you have shown, the problem should be in addRecord javascript function on top of that id is same i.e. 'name' for all element....
You get the value from the form using $('#name').val(); or document.getElementById('name'); and look all input has same id. If I am going right then pass value in addRecord function and use that value.

getting Checkbox value in php

When I pass the html form having checkboxes to a php page, when I retrieve them I get value true for all the boxes that I have checked, and false for unchecked boxes. I need to get the value only.
<?php
$contact=$_POST['contact'];
foreach ($contact as $conid){
echo $conid
}
?>
One possible approach is to set names like that:
<input type='checkbox' name='box[1]'>
<input type='checkbox' name='box[2]'>
<input type='checkbox' name='box[3]'>
This way you could access them in PHP with
foreach ($_POST['box'] as $id=>$checked){
if ($checked =='on')
//process your id
}
The second approach is more clean, I think. Set value's attributes on checkboxes:
<input type='checkbox' name='box[]' value='1'>
<input type='checkbox' name='box[]' value='2'>
<input type='checkbox' name='box[]' value='3'>
With this you'll receive only checked values:
foreach ($_POST['box'] as $id){
//process your id
}

Categories