php: get post results from multiple forms through one submit - php

I have a table with a list of clients, then a "modify" button then a "delete" button, and then a "add to mailing list" checkbox, each of these elements is its own form. There is also a selector at the end to chose which mailing list to add the address to.
My problem is with these checkboxes.
I have one submit button at the end of the table to add all of the checked emails to a file, but it only works on the last email address.
Here is the code for the checkboxes:
echo "<td>";
echo "<form action=\"\" method=\"post\">";
if($email!="" && $email!=null){
echo "<label class=\"checkbox-inline\">
<input type=\"checkbox\" name=\"mailing[]\" value=\"".$email[0][0]."\" title=\"".$email[0][0]."\"><span><i class=\"glyphicon glyphicon-envelope\"></i></span></label>";
}else{
echo "<label class=\"checkbox-inline\">
<input type=\"checkbox\" disabled><span><i class=\"glyphicon glyphicon-envelope\"></i></span></label>";
}
echo "</form>";
echo "</td>";
//... more code
//selector + submit
<form action="" method="post">
<div class="col-sm-6 pull-right">
<label class="control-label">Ajouter ces adresses à la liste:</label>
<select class="form-control" name="liste">
<option value=""></option>
<?php
$linkpdo = openConnection();
$req = $linkpdo->prepare('SELECT * FROM AERA.mailing');
$req->execute();
$lists = $req->fetchAll();
if(count($lists)<>0){
for ($i = 0; $i < count($lists) ; $i++){
echo "<option value=\"".$lists[$i][0]."\">";
echo $lists[$i][1];
echo "</option>";
}
}
?>
</select>
</br>
<button type="submit" class="btn btn-info" id="clickAll">Ajouter</button>
How can I get all of the checkbox values? Or is there another way to go about it? Thanks
UPDATE
Here is how I am currently handling the results:
if(!empty($_POST['mailing'])) {
foreach ($_POST['mailing'] as $key => $value){
$mailing=$_POST['mailing'][$key];
if(stripos(file_get_contents($filename), $mailing)!== false){
echo '<div class="container">
<div class="col-sm-6 col-sm-offset-3 alert alert-info">
<p class="text-center">Vous avez essayé d\'insérer des doublons.</br>Ces valeurs n\'ont pas été prises en compte</p>
</div>
</div>';
}else{
$stringIn.=$mailing.", ";
}
}
}

Why don't you make a html form and than a php script?
you can see an example here: Getting checkbox values on submit

Related

html form post is not sending variable data

After clicking "delete" (submit): I can tell that it processes the form because I get the error "You forgot to select a vendor to delete.".
The form itself is generated using PHP. When I check the HTML output, I see that the values are there. But when it gets to POST, nothing carries over. I'm testing and using a local host, so other than the existing action link, I'm not sure what else I can modify.
I have the exact same code in another delete form and it works fine. I literally copied the other form to create this one and just changed the SQL/Variable names.
PHP Code:
#delete vendor
require('includes/mysqli_connect.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = array(); //array to collect errors
//check to ensure fields are filled out
if (!empty($_POST['vendor'])) {
$vendor = trim($_POST['vendor']);
}
else {
$errors[] = "<p>You forgot to select a vendor to delete</p>";
}
if (empty($errors)) {
//insert information into appt table
$query = "UPDATE vendors_t SET active = 0 WHERE vendorID = $vendor;";
$result = mysqli_query($dbc, $query);
if($result){
echo '<div class="alert alert-success" role="alert">';
echo '<h2>Thank you!</h2>';
echo '<p> Your request to delete vendorID ' . $customerID . 'has been completed </p></div>';
} else {
echo "Error updating record: " . mysqli_error($dbc);
}
} else{
echo '<div class="alert alert-danger" role="alert"><br>';
echo '<h2>Errors Detected</h2>';
echo '<p>The following errors occured:<br/>';
foreach ($errors as $msg) {
echo "$msg<br/>\n";
}
echo '</p><p>Please try again. </p></div>';
}
}
HTML Code
<div class="container standout" >
<div class="col-lg-12">
<h2>Delete a vendor</h2>
<p> Please select a vendor to delete.</p>
</div>
<div class="row">
<form action="index.php?pagelet=deletevendor" method="post">
<div class="form-group col-md-4">
<label for="vendor">Vendors</label>
<select name="vendor" class="form-control">
<option name="vendor" selected disabled>Choose here</option>
<?php
$query = "SELECT vendorID, contactFname, contactLname, businessName FROM vendors_T WHERE active = 1;";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<option name="vendor" value="'.$row['vendorID'].'">'
.$row['vendorID'].' '.$row['businessName']
.' '.$row['contactFname'].' '.$row['contactLname']
.'</option>';
}?>
</select>
<br>
<br>
<input class="btn btn-primary" id="submit" type="submit" value="Delete">
</div>
</div>
</form>
</div>
Your option names should be the value of the vendor ID (I presume) and certainly not "vendor".
removing name="vendor" from options & removing selected both work.
After removing name="vendor" - I added selected back and it still works.
Thank you!!

Simple Form does not work

Trying to make a simple form to send an email to a database. New to php. The isset doesn't trigger so i guess the post isn't posting?! Heres what i have tried. I have tried with the get/post require etc. Thanks for any help.
<form class='form-horizontal' action='includes/news-letter.php' method='POST'>
<div class='form-group'>
<div class='col-xs-8 col-xs-offset-2'>
<label for='' class='control-label'>Email</label>
<input type='text' class='form-control input-newsletter' name='newsletterEmail' id='inputPassword' placeholder='someone#somwhere.com'>
</div>
</div>
<div class='form-group'>
<div class='col-xs-6 col-xs-offset-4'>
<button type='submit' class='btn btn-default btn-newsletter' name='newsletterEmail'>Get News Letter</button>
</div>
</div>
</form>
<?php
require_once("dbConnection.php");
news_letter();
function news_letter(){
// if(isset($_POST["newsletterEmail"]))
if (isset($_POST["newsletterEmail"]) && !empty($_POST["newsletterEmail"])){
// print_r($_POST);
echo "IS SET";
$newsletterEmail = $_POST['newsletterEmail'];
echo $newsletterEmail;
if ($newsletterEmail==''){
echo "<h6 class='alert alert-danger'>Please fill in field.</h6><br>";
}else{
$query = "INSERT INTO newsletterEmail ";
$query .= "(newsletterEmail)";
$query .= "VALUES (:newsletterEmail)";
$ps = $db->prepare($query);
$ps->execute(array(
"newsletterEmail" => $newsletterEmail,
));
}
}
};
?>
Your submit button has the same name that your textfield, that why it doesn't work.
You don't have to check either "isset" and "!empty" (not empty) in fact if the value is set it's not empty so just do this :
if(!empty($_POST["newsletterEmail"])){
echo "IS SET";
}

How to detect from which button a form is submitted from a loop

I made a loop that creates multiple divs. In the divs you can find a form for submitting something.
I now want to detect from which button a form has been submitted. How can I do this?
<?php
if(!empty($aw)){
$element = "<div class='agendadiv'>
<h2>WEEK</h2>
<form method='post'>
<label for='oefs'>Welke oefeningen zijn er gepland voor deze week?</label></br>
<input type='text' name='oefs'>
<button id='oefsklaar' type='submit' name='oef'>Voeg Toe</button>
</form>
</div>";
$count = $aw;
foreach( range(1,$count) as $item){
echo $element;
}
}else{
?> <h2>Maak een agenda aan aub.</h2> <?php
}
?>
The $aw variable holds the numbers of divs that have to be created. I get this value from the database.
The code works but I want to be able to detect from which button a form has been submitted so I can add that in the database for when I want to show the submitted value I can show the value/div.
Otherwise you can see the entered value in div 1 in all divs. I only want to be able to see the value entered in div 1 in div1.
You need to make unique your submit button name like:-
<?php
if(!empty($aw)){
$count = $aw;
$i=1;
foreach( range(1,$count) as $item){
$element = "<div class='agendadiv'>
<h2>WEEK</h2>
<form method='post'>
<label for='oefs'>Welke oefeningen zijn er gepland voor deze week?</label></br>
<input type='text' name='oefs'>
<button id='oefsklaar' type='submit' name='oef$i'>Voeg Toe</button>
</form>
</div>";
echo $element;
$i++;
}
}else{
?> <h2>Maak een agenda aan aub.</h2> <?php
}
?>
I have added a counter that added to button name you can also use for form name
you can check like : $_POST['oef1'] is set and so on
For add more with element:-
$element = "<div class='agendadiv'>
<h2>WEEK</h2>
<form method='post'>
<label for='oefs'>Welke oefeningen zijn er gepland voor deze week?</label></br>
<input type='text' name='oefs'>
<button id='oefsklaar' type='submit' name='oef$i'>Voeg Toe</button>";
if (!empty($alloefs)) {
foreach($alloefs as $a) {
$element .="<ul> <li>".$a['oefening']."</li> </br> </ul>";
} }
$element .= "</form>
</div>";

how to make drop-down form to remember input in php

im creating a form which will display data from database, everything is set so far, but i want the form to remember the data in case if user has to write it down again in case of error. I found some similar code concerning only html, but when including php to display a form i find some difficulties for code to remember the last input (current problem is only concerning drop down selection list):
$Type = $_POST['petType']; //this should remember last input
<?php
/*upload form and drop-down selection list*/
echo "
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>\n
<option value='-1'>Type:</option>";
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
echo "<option value='$petType' ";
if($Type == '$petType')
{
echo "? selected='selected'";
// i need to make selected true only for last selected option,
//and redisplay it in the same form again
}
echo ">$petType </option>";
}
echo "</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
?>
try this:
<?php
$Type = $_POST['petType']; //this should remember last input
?>
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' <?php echo $Type == $petType ? "selected=selected":"";?> ><?php echo $petType;?> </option>
}
</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
sidenote: make html and php different , it makes things easier.
You are comparing $Type to a string not a variable.
Try
if($Type == $petType)

select ID so you can create a dynamic query

I am new to php and phpmyadmin and I have a problem with a web application that I am making.
I know how to enter items in the DB and how to displays them. But now I have to do both.
I have to make sure they select a name from the option tag that comes from the db. I need the ID of this name so I can create a new query
/* Hier I get the data*/
<form class="form-signin" role="form" name="SelecteerKlas" method="get" action="<?php $_PHP_SELF ?>" >
<select>
<?php
if (!empty($data)) {
foreach ( $data as $var )
{
$k_id=$var['k_id'];
echo "<option name='klasID' value='$k_id'>",$var['k_leerjaar'],$var['k_naam'],"</option>";
}
}else{
$feedback= '<p class="alert alert-danger">Momenteel staan er nog geen klassen in de database!</p>';
echo $feedback;
}
?>
</select>
<input name="btnSelectKlas" type="submit" id="btnSelectKlas" value="Selecteer klas" >
</form>
But I don' know how to proceed ...
you have syntax errors in concate string in below line:
echo "<option name='klasID' value='$k_id'>",$var['k_leerjaar'],$var['k_naam'],"</option>";
and you also need to echo your message outside of the form if array is empty.
Try like this:
<?php
if (!empty($data)) {
?>
<form class="form-signin" role="form" name="SelecteerKlas" method="get" action="<?php $_PHP_SELF ?>" >
<select name="my_select">
<?php
foreach ( $data as $var )
{
$k_id=$var['k_id'];
echo "<option name='klasID' value='$k_id'>".$var['k_leerjaar'].$var['k_naam']."</option>";
}
}
?>
</select>
<input name="btnSelectKlas" type="submit" id="btnSelectKlas" value="Selecteer klas" >
</form>
<?php
else{
$feedback= '<p class="alert alert-danger">Momenteel staan er nog geen klassen in de database!</p>';
echo $feedback;
}
?>
you need check if form is submit after submitting the form you can get select value by $_REQUEST['my_select']

Categories