select ID so you can create a dynamic query - php

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']

Related

why Isn't my form working

I'm trying to use a listbox form to query the database but it's not showing anything. The idea is that I've queried the database to fill the form with the names of suburbs and then, selecting a suburb will query the database again to return the names of parks in that suburb. When I use the search form it doesn't return anything.
this is the form:
<p>Select Suburb to search</p>
<form method="post" action="suburb_search.php" id="search">
<select>
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
<input type="submit" name="search" value="Search" />
</form>
</div>
This is where it should use the results of the form to query the database but its not working:
<?php
$searchRequest = False;
if (isset($_GET['suburb'])){
$search = $_GET['suburb'];
$sql2 = "SELECT * FROM park_list WHERE suburb=$search";
$result2 = $db->query($sql2);
if($message){
echo "<p>$message</>";
} else {
?>
<div class="form">
<?php
while ($row2 = $result2->fetch_assoc()){
?>
<div class="results">
<h2><?php echo $row2['park_name'];?></h2>
<?php
}
}
} ?>
give a name for your select element as
<select name="suburb">
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
and you are giving form method as POST but accepting data in GET in your php code, change this
if (isset($_GET['suburb'])){
to
if (isset($_POST['suburb'])){

php: get post results from multiple forms through one submit

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

how to redirect data on same page with data for update in php

I am new in php my query is, how to display data in a text box after an insertion operation. I've a page where I can set the input fields and when I click save button the inputs will be inserted in database and will stay in the same page. This is a product page where a list of products are available and there is an edit and delete button in each row. Now i will click on edit button the data can be edited in the same page.. I'm creating an admin panel.
<div class="panel-body">
<form role="form" method="post" enctype="multipart/form-data" name="myform" action="<?php $_SESSION['PHP_SELF']?>">
<br/>
<label>Select Category:</label>
<div class="form-group">
<?php
include('config.php');
$query1=mysql_query("select catnm from addcategory");
?>
<select class=form-control m-b-10 name=catnm id=catnm>
<?php
echo"<option>---select---</option required>";
while($query=mysql_fetch_array($query1))
{
echo "<option value=$query[catnm]>$query[catnm]</option>";
}
?>
</select>
<br/>
<div class="form-group">
<input type="text" class="form-control" id="prodnm" placeholder="Product Name" name="prodnm" required value="<?php
echo $query4['prodnm'];
?> " />
</body>
<?php
//coding for insert .
$catnm=$_POST['catnm'];
$prodnm=$_POST['prodnm'];
$prod_dtl=$_POST['prod_dtl'];
//$updates=$_POST['downld'];
$date=$_POST['date'];
$query=("insert into addproduct(catnm,prodnm,prod_dtl,pdf,downld,date)
values('$catnm','$prodnm','$prod_dtl','$uploadPath','$updates','$date')");
$result = mysql_query($query,$conn);
echo"record inserted";
if($result)
{
header("location:addProduct.php");
}
?>
<?php
//coding for update.
include('config.php');
if(isset($_GET['id']))
{
$id=$_GET['id'];
echo $_SERVER['QUERY_STRING'];
if(isset($_POST['submit']))
{
$updates['downld'] = isset($_POST['downld']) ? 'Yes' : 'No';
$cat['catnm']=$_GET['catnm'];
$prodnm['prodnm']=$_GET['prodnm'];
if($query3)
{
header('location:addProduct.php');
}
}
$query1=mysql_query("select * from addproduct where id='$id'");
$query4=mysql_fetch_array($query1);
?>

php script with multiple buttons

The idea is the page in which you select a category from drop-down menu, then after you click remove button, new form shows(contains yes/no buttons) that asks you if you really want to remove selected category. Problem is the second yes/no script. It works on separate page, but on page with first form it doesn't echo anything nor does it remove a pet. Please help, thanks!
<?php
/*remove a category*/
include("connection.php");
?>
<html><head></head>
<body>
<?php
$PetListquery= "SELECT distinct petType From petType ORDER by petType";
$PetListResult= mysqli_query($cxn,$PetListquery) or die ("Couldn't execute query.");
?>
<div style="border:2px solid;">
<form method="POST" action="removeCategory.php">
<div align='left'>
Choose category you want to remove:
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' ><?php echo $petType;?> </option>
<?php }?>
</select>
</div>
<div>
<p><input type='submit' name='Remove' value='Remove Category' />
</div>
</div>
</form>
<?php
foreach($_POST as $field => $value)
{ //second form starts after if
if($field == 'petType')
{
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<div>
<input name="Yes" type="submit" value="Yes">
<input name="No" type="submit" value="No">
</div>
</form>
<?php
echo "Are you sure you want to delete selected category?";
//clicking any of these buttons doesn't display anything
if(isset($_POST['Yes']))
{
echo "yes";
$DeleteQuery= "DELETE From petType WHERE petType='$petType'";
$DeleteResult= mysqli_query($cxn,$DeleteQuery) or die ("Error1!");
}
if(isset($_POST['No']))
{
echo "No!";
}
}
}
?>
</body></html>

Form data not posting to uri

Can you tell me why when I click call my data is not posting to my action?
<body>
<?php
$login = '1236567';
$password = '10152930';
$officeNumber = array('0212177899','027899899','09111');
?>
<div id="wrapper">
<form method="POST" action="https://live.domain.co.nz/call.php?login=<?php echo $login; ?>&password=<?php echo $password; ?>&aparty=<?php echo $number; ?>phone&bparty=<?php echo $number;?>">
<?php
echo '<label for="officeNumbers">Office Number: </label>';
echo '<select name="officeNumbers">';
foreach($officeNumber as $number)
{
echo '<option value="'.$number.'">'.$number.'</option>';
}
echo '</select>';
?>
<label for="callTo">Call: </label><input type="text" id="callTo">
<input type="submit" value="Call">
</form>
</div>
</body>
</html>
Your text input:
<input type="text" id="callTo">
… has no name attribute, so it cannot be a successful control (and thus submit any data).
Give it a name attribute.

Categories