PHP and HTML Code after while loop not working - php

I have designed a form and a dropdown that fetches its option from MYSQL Database. But after getting the options the rest of the code like the button and other parts disappeared not showing. The rest of the code works fine every time I comment out the PHP Query.
<form action="" method="POST">
<label><b>Add Student to this User ID</b></label>
<select name="student_id" class="form-control">
<option value="0">--Select Student--</option>
<?php
$query = mysqli_query($con, "SELECT * FROM students ORDER BY first_name, last_name, Other_name ASC");
while ($row = mysqli_fetch_assoc($query)) { ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['first_name'] . ' ' . $row['last_name'] . ' ' . $row['Other_name'] ?></option>;
<?php } ?>
</select>
<br />
<input type="hidden" name="page_id" value="<?php echo $user_id; ?>">
<div class="row">
<div class="col-sm-3 col-sm-offset-4">
<p><input type="submit" class="btn btn-xs btn-success" name="submit" value="Add Student"></p>
</div>
</div>
</form>

There's a semicolon missing after $row['Other_name']

Related

How to post a different value from that shown in a select form input

On my webpage i have the below form. I am using some PHP to display the name of users in the select input. My question is this... how do I save down the id column (that I am selecting in the SQL) to the database instead of the name columns that I am displaying? All help greatly appreciated.
<form action='insertmodule.php' method='POST'>
<span class="form-group">
<input type="text" placeholder="Name" class="form-control" name="name" required/>
</span>
<br>
<div class="form-group">
<label for="exampleSelect1">Select the tutor for the module</label>
<select class="form-control" name="tutor">
<?php
include('../connections/conn.php');
$sql = mysqli_query($conn, "SELECT id, first_name, last_name FROM cater_users WHERE role = 2");
while ($row = $sql->fetch_assoc()){
echo "<option>" . $row['first_name'] . " ". $row['last_name'] . "</option>";
}
?>
</select>
</div>
<button type="submit" class="btn btn-primary" name='sign'>Create</button>
</form>
try this
echo sprintf('<option value="%s">%s %s</option>', $row['id'], $row['first_name'], $row['last_name']);

form POST method: if(isset($_POST['btn-save'] not returning true after button click (possibly due to angular routing(?))

I'm fairly certain it couldn't be the angular routing since the button has both ng-click and name="btn-save", one for directing back to the main page which works and the other is for the php section which doesnt work. I have spent quite a few days solving it.
I thought about passing form data to an ng-model then to the js file for angular to post it via a php file as the mysql call, then there are some values in my dropdown that couldn't be passed over so I shelved the idea.
Can anyone figure out what is wrong with it? Sorry of the code is messy.
addSales.php
<?php
require_once 'database.php';
//if submit button is clicked submit
if(isset($_POST['btn-save']))
{
$data = json_decode(file_get_contents("php://input"));
//sotred variables from form
$customer_name= $_POST['customer_name'];
$item_id= $_POST['item_name'];
//retrieve
$item_name = mysqli_query($connect,"SELECT ItemName FROM Items WHERE ItemID = '$item_id");
$country= $_POST['country_name'];
$quantity= $_POST['item_quantity'];
$price= $_POST['item_price'];
$sales_date= $_POST['sales_date'];
//insert sales record
mysqli_query($connect,"INSERT INTO Sales (CustomerName, ItemID, ItemName, Country, Quantity, Price, SalesDate) VALUES('Awful Days', 17, 'Xanax', 'Singapore', 1, 48.3, 2012-01-02)");
//mysqli_query($connect,"INSERT INTO Sales (CustomerName, ItemID, ItemName, Country, Quantity, Price, SalesDate) VALUES('$customer_name', $item_id, '$item_name', '$country_name', $quantity, $price, $sales_date)");
//update stock count
mysqli_query($connect,"UPDATE Items SET StockLeft = StockLeft - '$quantity' WHERE ItemID = '$item_id'");
//check stock after each record add
$stockchecker=mysqli_query($connect,"SELECT ItemName FROM Items WHERE StockLeft <= 5");
if($stockchecker != NULL)
{
$message = $stockchecker + "is running out of stock!";
echo "<script type='text/javascript'>alert('$message');</script>";
}
//return to index main page
echo "
<!DOCTYPE html>
<script>
function redir()
{
alert('Record successfully added.');
window.location.assign('index.php');
}
</script>
<body onload='redir();'></body>";
}
?>
<form method="post">
<div class="form-group">
<label for="custName">Customer Name:</label>
<input type="text" name="customer_name" class="form-control" required/>
</div>
<div class="form-group">
<label for="itemName">Item Purchased:</label>
<?php include "database.php";
$result = mysql_query("SELECT ItemID, ItemName FROM Items");
echo "<select name='item_name' class='form-control'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['ItemID'] . "'>". $row['ItemID'] . " - " . $row['ItemName'] ."</option>";
}
echo "</select>";
?>
</div>
<div class="form-group">
<label for="Country">Country:</label>
<select name="country_name" class="form-control">
<option value="Malaysia">Malaysia</option>
<option value="Thailand">Thailand</option>
<option value="Singaore">Singapore</option>
<option value="Phillipines">Phillipines</option>
<option value="Vietnam">Vietnam</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-group">
<label for="Quantity">Quantity:</label>
<input type="text" name="item_quantity" class="form-control" required/>
</div>
<div class="form-group">
<label for="itemPrice">Total Price (MYR):</label>
<input type="text" name="item_price" class="form-control" required/>
</div>
<div class="form-group">
<label for="dateSold">Date:</label>
<input type='text' name="sales_date" class="form-control" placeholder="dd/mm/yyyy"/>
</div>
<div class="form-group">
<button ng-click="save()" type="submit" name="btn-save" class="btn btn-primary">Save</button>
<button ng-click="cancel()" class="btn btn-primary">Cancel</button>
</div>

PHP Full search form with select fields

having this problem for more than three days to fix.
I'm creating a form that lets the user search with some conditions.
Why the values of company and category are not passing to the URL when using the get method?
<form class="main-searchbox-container" role="search" method="get" action="search-results.php">
<div class="form-group" id="main-searchbox">
<input id="target" type="text" class="form-control main-search-input" placeholder="Search..." name="target" value="<?php if (isset($_POST['target'])) echo $_POST['target']; ?>" />
</div>
<div class="selection-container">
<select name="company" class="header-selection" form="main-searchbox">
<option value="company" name="company">Company</option>
</select>
</div>
<div class="selection-container">
<select name="category" class="header-selection" form="main-searchbox">
<?php
// Make the query:
$sql = "select Business_Category from business_categories order by Business_Category ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['Business_Category'].'" name="'.$row['Business_Category'].'">'.$row['Business_Category'].'</option>';
}
} else { echo "----";} // End
?>
</select>
</div>
<input id="submit" class="btn btn-default main-search-button" type="submit" name="submit" value="Search">
</form>
It must be quite straight forward to fix this and with your feedback can fix that easier.
Thank you for your time.
It worked by removing form="main-searchbox" present in both <select> elements (inside of <div class="selection-container">) as there is not a matching id with that name on the <form>, as RiggsFolly said in the comments.
See the working code:
<form class="main-searchbox-container" role="search" method="get" action="search-results.php">
<div class="form-group" id="main-searchbox">
<input id="target" type="text" class="form-control main-search-input" placeholder="Search..." name="target" value="<?php if (isset($_POST['target'])) echo $_POST['target']; ?>" />
</div>
<div class="selection-container">
<select name="company" class="header-selection">
<option value="company" name="company">Company</option>
</select>
</div>
<div class="selection-container">
<select name="category" class="header-selection">
<?php
// Make the query:
$sql = "select Business_Category from business_categories order by Business_Category ASC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['Business_Category'].'" name="'.$row['Business_Category'].'">'.$row['Business_Category'].'</option>';
}
} else { echo "----";} // End
?>
</select>
</div>
<input id="submit" class="btn btn-default main-search-button" type="submit" name="submit" value="Search">
</form>

Multiple buttons on webpage are invoking all button actions

I have two submission buttons on my webpage which are assigned to do different things. My problem is that clicking 1 of the buttons is invoking the actions of both buttons. How can I make it so that clicking 1 button only invokes the associated form submission action?
(This is my first day using php).
First form:
<form id="frm1" onsubmit="document.getElementById('frm1').submit();">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)">
</input>
<input name="id" type="hidden" value="<?php echo $ID; ?>">
<input name="addTime" type="hidden" value="true">
<button style="width:40px" type="submit" class='btn btn-success pull-right'>
<i class="icon-plus"></i>
</button
</form>
Second form:
<form id="frm2" onsubmit="document.getElementById('frm2').submit();">
<center>
Edit Group:
<?php
#
$styles = sqlToQuery("SELECT groupID, Name from sha_scheduler_dev.sha_scheduler_map_job_group");
echo '<select style="width:100" name="groupNum">';
for($i = 0; $i < count($styles); $i ++){
echo ' <option style="width:100" value="'.$styles[$i][0].'">'.$styles[$i][1].'</option>';
}
echo '</select><br>';
?>
<input name="editGroup" type="hidden" value="true"><br>
<button type="submit" class='btn btn-info pull-right'>
Submit Group
</button>
</form>
Checking if buttons are submitted:
if ($_GET['addTime']==='true'){
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (".$ID." ,'".$_GET['data']."')");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
if ($_GET['editGroup']==='true'){
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '".$_GET['groupNum']."' WHERE a.job_ID = ".$ID." ");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
In your first form, button tag is not closed properly, that's why first form is tag is not closed properly,use this,
<form id="frm1" onsubmit="document.getElementById('frm1').submit();">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)">
<input name="id" type="hidden" value="<?php echo $ID; ?>">
<input name="addTime" type="hidden" value="true">
<button style="width:40px" type="submit" class='btn btn-success pull-right'>
<i class="icon-plus"></i>
</button>
</center>
</form>
Also change your php code like this
if ( isset($_GET['addTime']) && $_GET['addTime']==='true'){
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (".$ID." ,'".$_GET['data']."')");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
if ( isset($_GET['editGroup']) && $_GET['editGroup']==='true'){
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '".$_GET['groupNum']."' WHERE a.job_ID = ".$ID." ");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
Give you're submit buttons a value and a name catch the different values using $_GET(in your case) or $_POST
Update: (just to give it a try maybe)
First Form
<form id="frm1" action="" method="post">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)" />
<input name="id" type="hidden" value="<?php echo $ID; ?>" />
<input style="width:50px" type="submit" class='btn btn-success pull-right' name="submit_form1" value="submit" />
<i class="icon-plus"></i>
</button>
</center>
</form>
Second Form
<form id="frm2" name="frm2" action="" method="post">
<center>
Edit Group:
<?php
$styles = sqlToQuery("SELECT groupID, Name from sha_scheduler_dev.sha_scheduler_map_job_group");
echo '<select style="width:100" name="groupNum">';
for ($i = 0; $i < count($styles); $i ++)
{
echo ' <option style="width:100" value="' . $styles[$i][0] . '">' . $styles[$i][1] . '</option>';
}
echo '</select><br>';
?>
<input type="submit" class='btn btn-info pull-right' name="submit_form2" value="Submit Group" />
</form>
PHP
<?php
if (filter_input(INPUT_POST, "submit_form1"))
{
// {Check for empty var on data}
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (" . $ID . " ,'" . $_GET['data'] . "')");
echo '<script>window.location.assign("' . $redir . '?id=' . $ID . '");</script>';
}
if (filter_input(INPUT_POST, "submit_form2"))
{
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '" . $_GET['groupNum'] . "' WHERE a.job_ID = " . $ID . " ");
echo '<script>window.location.assign("' . $redir . '?id=' . $ID . '");</script>';
}
?>
This is pretty tight and keeps your url clean.

get submit form action with array

i have a form based on database mysql ,just like this
<form name="" id="form1" method="GET" action="submit.php"></form>
<?php
include 'connection.php';
$myquery="SELECT * FROM `tbl` " ;
$params=mysql_query($myquery) or die (mysql_error());
while ($param=mysql_fetch_assoc($params))
{?>
<input name="<?php echo $param['id'] ?>" value="<?php echo $param['child_id'] ?>" />
<input type="checkbox" name="checkbox<?php echo $param['id'] ?>" value="Yes" form="form1" />
<input class="form-control text-center" form="form1" name="desc<?php echo $param['id'] ?>" value="<?php echo $param['desc'] ?>"/>
<input class="form-control text-center" form="form1" name="value <?php echo $param['id'] ?>" value="<?php echo $param['value'] ?>"/>
<select class="form-control" form="form1" id="" name="select<?php echo $param['id'] ?>">
<option value=""></option>
<option value="OK">OK</option>
<option value="NOK">NOK</option>
</select>
<?php } ?>
<button type="submit" class="btn btn-success btn-s-md btn-rounded" form="form1"><i class="icon-save"></i>Save</button>
how can i submit (do update ) to mysql database with this form , i am a newbie in php .
thanks
So yes you have to add the balise form at this end. Then I advice you to add a hidden value with your id.
<input type="hidden" name"id" value="<?php echo $param['id'] ; ?>">
After ad in your page submit.php,
<?php
include 'connection.php';
if (isset($_POST["id"])){
$id=$_POST["id"];#so you can recover your id
$myquery="UPDATE ... " ;
mysql_query($myquery) or die (mysql_error());
echo "update success";# you can add whatever you want here
}?>
You have to wrap all your form elements into the <form> tag. So place your closing tag (</form>) at the end of your form.
<form name="" id="form1" method="GET" action="submit.php">
<input name="..." />
<select>
<option value="...">...</option>
...
</select>
<button>...</button>
</form>
By the way: PHP's mysql-functions are deprecated. Use mysqli instead!

Categories