determine which dynamically generated checkboxes are checked - php

I'm trying to make a form with checkboxes which should subscribe a user to a forum in the database eventually.
I use this code to dynamically create a list of subscribed/unsubscribed forums for the current user:
//$checkboxes = array();
echo' <form action="" method="post">';
while($unsubscrlist = mysql_fetch_assoc($sublist))
{
//$checkboxes[] = $unsubscrlist['Name'];
echo '<input type="checkbox" checked="checked" name="subscrform[]"
value="' .$unsubscrlist['Name']. ' "/>' .$unsubscrlist['Name']. ' <br />';
}
while($subscrlist = mysql_fetch_assoc($notsublist))
{
// $checkboxes[] = $subscrlist['Name'];
echo '<input type="checkbox" name"subscrform[]"
value="' .$subscrlist['Name']. '"/>' .$subscrlist['Name']. '<br />';
}
echo '<br />
<input type="submit" value="Submit" />
</form>';
Then, to determine which checkboxes are checked I use this code:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['subscrform']))
{
//echo 'getshereeee';
foreach($_POST['subscrform'] as $value)
{
//echo 'getshereeee';
echo $value;
}
}
Now the first checkboxes - to which the user is already subscribed -are output as value by this code, while the second list of checked checkboxes is never output as value somehow..
I thought this might had to do with using the same name twice. So I changed the first list to unsubscrform and did the check loop loop for both but still only get the first list as output values.
I feel like I'm missing something simple but can't really find out what. I would very much appreciate your help!

I just found a solution to my problem!
by changing
echo' <form action="" method="post">';
to
echo' <form name="subscrform" action="" method="post">';

Related

Can't pass hidden form value from database to PHP if-statement

My database table (Related) contains 3 columns:
related_id
article_id
object_id
It's a table which keeps track of relationships between articles and objects. I have stripped the code. Now it just contains the delete button (x). If someone press that button I want the user to be redirected to if(isset($_POST['deleteRelated'])) to get a "Are you sure"-message etc. But the hidden ID isn't passed correctly. The last related_id in the table is 29. When I try to echo out the hidden ID i just get 29 for every delete button (x).
The complete version of the code below gives me a table with the article title, object title and the delete button (x). Because of a submit button can't pass a value by itself I need a hidden value. But when I pass it by pressing the delete button (x) i just gets 29 every time.
Code
if(isset($_POST['deleteRelated'])) {
echo $_POST['hidden-id']; // Will just display the last 'related_id' value.
else {
echo '
<form method="post">';
$stmt = $db->prepare("SELECT * FROM Related");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $related) {
echo '
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">';
}
echo '
</form>';
}
If I type:
<input type="submit" name="deleteRelated" value="' . $related['related_id'] . '">
It will display the correct value from the database instead of the x for the delete/submit button. But when I press the delete/submit button I just get the last related_id which is 29.
Can someone solve this problem? Should not be that hard?
Explanation
You have one <form> for the entire table, which includes (say) a dozen <input type="hidden" name="hidden-id" value="...">s.
Those values will ALL be sent to the server when the form is submitted (how do you expect it to know to only send the hidden-id which is next to the specific submit button that was pressed?) This is why you are only seeing the last hidden-id – they are all being sent, so the final one overrides/wins.
Solution
One solution would be to have a <form> per row instead of one <form> for the whole table:
foreach ($result as $related) {
echo '<form method="POST" action="...">';
echo '
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">';
echo '</form>';
}
That way, only the hidden-id value of the pressed button would be sent.
Alternatively, you really don't need a form for each row, you could use a button here instead and ditching those hidden inputs.
Example:
foreach ($result as $related) {
echo '<button type="submit" name="deleteRelated" value="' . $related['related_id'] . '">Delete</button>';
}
So now each value of that pressed button on each row will go to:
$related_id = $_POST['deleteRelated'];
You will have to put the in the foreach.
In your code, you have one form with a lot of button submit.
Try this:
foreach ($result as $related) {
echo '
<form method="POST" action="...">
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">
</form>';}
If I understood you right, why do you need multiple hidden inputs?
you might want to put this input once:
<input type="hidden" name="hidden-id" value="" />
Next, change the submit button to something like this:
echo '<input type="submit" name="deleteRelated" value="x" onclick="setValue('" . $related['related_id'] . "'); />';
And use a js function:
setValue(val) {
document.getElementsByName("hidden-id")[0].value = val;
}
Easy solution: Put your form tag inside foreach loop
$stmt = $db->prepare("SELECT * FROM Related");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $related)
{
echo '<form method="post">';
echo '
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">';
echo '</form>';
}

PHP self-form variable?

I was wondering if it is possible to use a variable from a form as its own URL... its hard to explain in my opinion, heres an example:
matchmaking.php:
$search_summoner = $_POST['search_summoner'];
echo '<form method="post" action="matchmaking.php?search=' . $search_summoner . '">';
echo '<input type="text" name="search_summoner">';
echo '<input type="submit">';
echo '</form>';
As you can see the action sends it back to matchmaking.php but with a variable from the form which was just submitted. The code above, which I have tried, didn't seem to work; So I wondered if anyone else had any ideas on how to do this...
Thanks for the help in advance
using jquery you could set the action parameter like this (add an ID "search" to the form)
$('input[name="search_summoner"]').on('keyup'function(){
$('form#search').attr('action','matchmaking.php?search='+$(this).val());
});
So, while typing, the action parameter will be extended by every character of the typed search string.
Try this;
<?php
if(isset($_POST['search_summoner']) && $_POST['search_summoner'] != '')
{
$search_summoner = $_POST['search_summoner'];
}
else
{
$search_summoner = "";
}
echo $search_summoner; // This displays your Query
?>
<html>
<body>
<form method="post" action=<?php echo $_SERVER['PHP_SELF']; ?>>
<input type="text" name="search_summoner">
<input type="submit">
</form>
</body>
</html>
If I understand you right, I think you want to do something like this:
$search_summoner = $_POST['search_summoner'];
echo '<form method="post" action="matchmaking.php">';
echo '<input type="hidden" name="search" value="'.$search_summoner.'"/>';
echo '<input type="text" name="search_summoner">';
echo '<input type="submit">';
echo '</form>';

PHP List of records from filemaker select button

First, thank you for reading this.
I am just starting php and I am tying to make a site using FileMaker to display and enter information.
I have the php connecting to my database, then a search page using a form, then it displays a list of records. I would like to make a "button" that will select one record then display related records.
This is where my trouble is. I do not know how to make a form that will save either the record_Id or key field to then display the next page.
I am using a foreach loop to display the list in a table:
$records = $result->getRecords();
echo '<table border="1">';
echo '<tr>';
echo '<th>Company</th>';
echo '<th>Id Num</th>';
echo '<th>Choose</th>';
echo '</tr>';
foreach ($records as $record) {
echo '<tr>';
echo '<td>'.$record->getField('Company').'</td>';
echo '<td>'.$record->getField('K_Medical').'</td>';
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
echo '</form></td>';
echo '</tr>';
}
echo '</table>';
As you can see I have tried to use a hidden form field to get the key field of the record, but the page dose not work. I get an error 500 when I try to view it in a browser.
Any help would be greatly appreciated! If I have not provided enough information please let me know.
Replace :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
By :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='.$record->getField('K_Medical').'/>
<input type="submit" />
</form>';
You have a quotes and concatenation errors.

Multiple Forms with Different Actions using Submit Buttons

I have Two forms in my index.php and am trying to get them to submit and POST their data seperatly
Form 1
echo '<form action="process.php?type=news" method="post">';
echo '<table cellspacing="0"><tr><th>';
echo 'Add News to News Feed';
echo '</th></tr><tr><td>';
echo 'Title:<br /><input name="newstitle" type="text" id="add" style="height:16px;width:525px;" size="80" maxlength="186" /><br />';
echo 'Main Body Text:<br /><textarea name="newsfeed" id="add" style="width:525px;height:78px;" maxlength="2000" ></textarea>';
echo '<input style="float:right;margin-top:5px;" id="button" type="submit" value="Submit" />';
echo '</td></tr></table></from>';
And Form 2
echo '<form action="process.php?type=suggest" method="post">';
echo '<table cellspacing="0"><tr><th>';
echo 'Suggest Additions to the Intranet';
echo '</th></tr><tr><td>';
echo '<textarea name="suggest" id="add" style="width:330px;height:60px;" maxlength="800" ></textarea>';
echo '<input style="float:right;margin-top:5px;" id="button" type="submit" value="Submit" />';
echo '</td></tr></table></from>';
I want these both to post and do the action after pressing the submit button, but currently the second form submit to the first forms action
How can i fix this???
EDIT: Also i am using .PHP for both the index and process page then using it to echo the forms onto the page
Also here is the process.php data
$type=$_REQUEST['type'];
$suggest=$_POST['suggest'];
$newstitle=$_POST['newstitle'];
$news=mysql_real_escape_string($_POST['newsfeed']);
if ($type == "news")
{
$sql=mysql_query("SELECT * FROM newsfeed WHERE title = ('$newstitle')");
$number_of_rows = mysql_num_rows($sql);
if ($number_of_rows > 0)
{
echo 'This Title is Already Taken';
}
else
{
$sql="INSERT INTO newsfeed (title, news) VALUES ('$newstitle','$news')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
exit;
}
}
elseif ($type == "suggest")
{
$sql="INSERT INTO suggestions VALUES ('$suggest')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
exit;
}
Could it be that you are not closing the form with </form>, but with </from>, so the code of the second form is in fact still inside the first form since the tags are never closed?
try giving the submit an on click for each form so in its submit buttons html put this:
onclick="document.forms["myform1"].submit();"
and for form 2 put
onclick="document.forms["myform2"].submit();"
Im not sure exactly what you would like to achieve, so heres my assumptions,
1. your form submits wrongly, eg, second form submits the first form's data
or
2. you would like to do both submits 1 after another.
FOR 1:
Do this.
<form id="form1" action="process.php" method="POST">
<input type="hidden" name="type" value="news">
....
</form>
Note the spelling
/form not /from
<form id="form2" action="process.php" method="POST">
<input type="hidden" name="type" value="suggest">
....
</form>
Note the spelling
/form not /from
Some pointers:
Instead of echoing out so many times, echoing out once is suffice! Remember echos slow down your script. Also I would not pass the type as a $_GET, post is always safer for forms, you can use a hidden field to add the type:
<?php
echo '
<form action="process.php" method="post">
<input type="hidden" name="type" value="news">
...
...
</form>';//Not </from>
?>
Then with the PHP part use a switch case instead of if elses, it will be easier to add features then splitting with another ifelse, plus the default block can be used for anything that dont match like the last else in an if else statement.
<?php
$type = $_POST['type'];
switch($type){
case "news":
...
break;
case "suggest":
...
break;
default:
//default if different type or no type is set
break;
}
?>
Also move over to PDO or prepared mysqli_ functions for your mySQL stuff its safer.

How to list users based on checkbox selection?

I have a text field and two checkboxes, I need to list users based on the selection. Can anyone show me an example.
See:
Enumerate all Check Box in PHP
<input name="rows[]" value="someVal" type="checkbox" />
<input name="rows[]" value="anotherVal" type="checkbox" />
<?php
// $_POST['rows'] contains the values of all checked checkboxes
//if something has been checked
if(isset($_POST['rows'])) {
//loop over each checked value
foreach ($_POST['rows'] as $row) {
echo $row . '<br />';
}
}
?>
if (isset($_POST['mycheckbox']))
{
draw_selectionCheckboxChecked();
}
else
{
draw_NoCheckbox();
}

Categories