I am making a student sign-up form using html and php.
First you are asked to insert your name, password and email and when you click submit, it takes you to another page (ChooseDepartment.php) in which you get a list of all departments from my database to choose your own.
Now, I am a total newbie, so here is the part of my php code that I am stuck with in ChooseDepartment.php:
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo </form>;
}
I am trying to make the value of the radio button carry the value of the
the department id so I can then update my database with the student's department which is currently NULL, but I can't figure out how to use both html and php at the very same line correctly! This gives me syntax error!
as you're in PHP, so you don't need to open and close PHP tag.
The reason you're getting Syntax error is just because you're not manipulating string properly.
error is with this line
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
^ here ^ here
So you need to remove the PHP tags and need to concatenate string properly like:
echo '<input type="radio" name="department" value="'.$row['DEPT_ID']. '">';
and with this one
echo </form>;
you're missing quotes around form tag. So it should be,
echo '</form>';
There are some other typos are as well, so your final code will be look like this.
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
//echo "<br />"; add this <br /> tag to next echo
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
//or you can do this way
//echo "<input type='radio' name='department' value='$row[DEPT_ID]'><br />";
//echo "<br />"; appended in upper statement.
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
//echo </form>; closed already(above statement).
}
and without comments, more cleaner :)
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
}
Take a look at string operators
http://php.net/manual/en/language.operators.string.php
You can combine two strings in php with a dot, so that part of your code would become this:
{
echo $row['NAME'];
echo '<input type="radio" name="department" value="'.$row['DEPT_ID'].'">';
echo "<br />";
}
No need to open php tag again
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0) {
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while ($row = mysql_fetch_array($ShowPossibleDep)) {
echo $row['NAME'];
echo '<input type="radio" name="department" value="' . $row['DEPT_ID'] .'">';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo "</form>";
}
Related
I would like some help printing what is in a certain textbox that was created by an echo command.
while($row = $result->fetch_assoc()){
$stringTest = $row['Price'];
$AssetId = $row['AssetId'];
echo "<center><div> <h3>Cost: ".$stringTest."";
echo '<form action="" method="get"><input type="text" name="uid">';
echo "</br><input class='myButton' type='submit' Name='Submit1' VALUE='I have bought'></a></form>";
/** ^ Input value I would like to get *//
echo "<a href='https://www.roblox.com/item-item?id=".$AssetId."' class='myButton'>Buy</a></h3></div></center>";
}
Use the code below to get the value from submit:
if(isset($_GET['Submit1'])) {
echo $_GET['Submit1'];
}
When the user clicks submit, it will echo the value of it.
If you want to print PHP element in a textbox you should put it in the value tag of the input
<?php
echo "<input type='text' value='" . $val . "'>";
?>
This form is a search form which allows the user to search for an event using the Venue and category fields which are scripted as dropdown boxes and the Price and event title as user input text boxes, as shown via the code if a keyword is entered which matches the fields on the database it should output all the related information for that event if any matches have been made on either search fields, the tickboxes allow the user to identify what criteria they would like to search with, if the tickbox field hasn't been checked then the SQL enquiry will not search for keywords with that corresponding field.
The issue is, it all seems to work fine except no results seem to show up for the Venue and Category fields if they was solely used to search for an event. But if I choose another field everything is outputting correctly including the venue and Category field.
DATABASE: http://i.imgur.com/d4uoXtE.jpg
HTML FORM
<form name="searchform" action ="PHP/searchfunction.php" method = "post" >
<h2>Event Search:</h2>
Use the Check Boxes to indicate which fields you watch to search with
<br /><br />
<h2>Search by Venue:</h2>
<?php
echo "<select name = 'venueName'>";
$queryresult2 = mysql_query($sql2) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult2)) {
echo "\n";
$venueID = $row['venueID'];
$venueName = $row['venueName'];
echo "<option value ='$venueName'";
echo ">$venueName</option>";
}# when the option selected matches the queryresult it will echo this
echo "</select>";
echo" <input type='checkbox' name='S_venueName'>";
mysql_free_result($queryresult2);
mysql_close($conn);
?>
<br /><br />
<h2>Search by Category:</h2>
<?php
include 'PHP/database_conn.php';
$sql3 ="SELECT catID, catDesc
FROM te_category";
echo "<select name = 'catdesc'>";
$queryresult3 = mysql_query($sql3) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult3)) {
echo "\n";
$catID = $row['catID'];
$catDesc = $row['catDesc'];
echo "<option value = '$catDesc'";
echo ">$catDesc </option>";
}
echo "</select>";
mysql_free_result($queryresult3);
mysql_close($conn);
?>
<input type="checkbox" name="S_catDes">
<br /><br />
<h2>Search By Price</h2>
<input type="text" name="S_price" />
<input type="checkbox" name="S_CheckPrice">
<br /><br />
<h2>Search By Event title</h2>
<input type="text" name="S_EventT" />
<input type="checkbox" name="S_EventTitle">
<br /><br />
<input name="update" type="submit" id="update" value="Search">
</form>
PHP CODE THAT DEALS WITH PROCESSING THE FORM DATA
<?php
include 'database_conn.php';
$venuename = $_POST['venueName']; //this is an integer
$catdesc = $_POST['catdesc']; //this is a string
$Price = $_POST['S_price'];
$EventT = $_POST['S_EventT'];
#the IF statements state if the tickbox is checked then search with these enquires
if (isset($_POST['S_VenueName'])) {
$sql = "SELECT * FROM te_venue WHERE venueName= '$venuename'";
}
if (isset($_POST['S_catDes'])) {
$sql = "SELECT * FROM te_category WHERE catID= '$catdesc'";
}
if (isset($_POST['S_CheckPrice'])) {
$sql = "SELECT * FROM te_events WHERE (eventPrice LIKE '%$Price%')";
}
if (isset($_POST['S_EventTitle'])) {
$sql = "SELECT * FROM te_events WHERE (eventTitle LIKE '%$EventT%')";
}
$queryresult = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult))
{
echo "Event Title: "; echo $row['eventTitle'];
echo "<br />";
echo "Event Description: "; echo $row['eventDescription'];
echo "<br />";
echo "Event Venue "; echo "$venuename";
echo "<br />";
echo "Event Category "; echo "$catdesc";
echo "<br />";
echo "Event Start Date "; echo $row['eventStartDate'];
echo "<br />";
echo "Event End Date "; echo $row['eventEndDate'];
echo "<br />";
echo "Event Price "; echo $row['eventPrice'];
echo "<br /><br />";
}
mysql_free_result($queryresult);
mysql_close($conn);
?>
Try using atleast MySQLi instead of deprecated MySQL. You can try this:
database_conn.php:
<?php
/* ESTABLISH YOUR CONNECTION. REPLACE THE NECESSARY DATA BELOW */
$con=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
?>
HTML Form:
<html>
<body>
<?php
include 'PHP/database_conn.php';
$sql2="SELECT venueID, venueName FROM te_venue"; /* PLEASE REPLACE THE NECESSARY DATA */
echo "<select name = 'venueName'>";
$queryresult2 = mysqli_query($con,$sql2);
while($row = mysqli_fetch_array($queryresult2)) {
echo "\n";
$venueID = mysqli_real_escape_string($con,$row['venueID']);
$venueName = mysqli_real_escape_string($con,$row['venueName']);
echo "<option value ='$venueName'>";
echo $venueName."</option>";
} /* when the option selected matches the queryresult it will echo this ?? */
echo "</select>";
echo "<input type='checkbox' name='S_venueName'>";
?>
<br><br>
<h2>Search by Category:</h2>
<?php
$sql3 ="SELECT catID, catDesc FROM te_category";
echo "<select name = 'catdesc'>";
$queryresult3 = mysqli_query($con,$sql3);
while($row = mysqli_fetch_array($queryresult3)) {
echo "\n";
$catID = mysqli_real_escape_string($con,$row['catID']);
$catDesc = mysqli_real_escape_string($con,$row['catDesc']);
echo "<option value = '$catDesc'>";
echo $catDesc."</option>";
}
echo "</select>";
?>
<input type="checkbox" name="S_catDes">
<br><br>
<h2>Search By Price</h2>
<input type="text" name="S_price" />
<input type="checkbox" name="S_CheckPrice">
<br><br>
<h2>Search By Event title</h2>
<input type="text" name="S_EventT" />
<input type="checkbox" name="S_EventTitle">
<br><br>
<input name="update" type="submit" id="update" value="Search">
</form>
</body>
</html>
PHP:
<?php
include 'database_conn.php';
$venuename = mysqli_real_escape_string($con,$_POST['venueName']); /* this is an integer */
$catdesc = mysqli_real_escape_string($con,$_POST['catdesc']); /* this is a string */
$Price = mysqli_real_escape_string($con,$_POST['S_price']);
$EventT = mysqli_real_escape_string($con,$_POST['S_EventT']);
/* SHOULD PRACTICE USING ESCAPE_STRING TO PREVENT SOME OF SQL INJECTIONS */
/* the IF statements state if the tickbox is checked then search with these enquires */
if (isset($_POST['S_VenueName'])) {
$sql = "SELECT * FROM te_venue WHERE venueName= '$venuename'";
}
if (isset($_POST['S_catDes'])) {
$sql = "SELECT * FROM te_category WHERE catID= '$catdesc'";
}
if (isset($_POST['S_CheckPrice'])) {
$sql = "SELECT * FROM te_events WHERE (eventPrice LIKE '%$Price%')";
}
if (isset($_POST['S_EventTitle'])) {
$sql = "SELECT * FROM te_events WHERE (eventTitle LIKE '%$EventT%')";
}
$queryresult = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($queryresult))
{
echo "Event Title: "; echo $row['eventTitle'];
echo "<br />";
echo "Event Description: "; echo $row['eventDescription'];
echo "<br />";
echo "Event Venue "; echo "$venuename";
echo "<br />";
echo "Event Category "; echo "$catdesc";
echo "<br />";
echo "Event Start Date "; echo $row['eventStartDate'];
echo "<br />";
echo "Event End Date "; echo $row['eventEndDate'];
echo "<br />";
echo "Event Price "; echo $row['eventPrice'];
echo "<br /><br />";
}
mysqli_close($conn);
?>
What if user checks all the check box? What would happen is, the last condition will be used. The first three conditions will be overwritten by the last condition.
If you use ELSE IF in those conditions, the first condition will be implemented.
My advice is to use radio button instead of check box and hope you gets the idea along the way.
Have you tried printing out your $sql query for debugging?
Try <input type="checkbox" name="S_catDes" value="checked">.
From memory checkboxes need a value field but I could be wrong. Hope this helps.
for some reason i cant find my error why i cant define my $id variable and paste it into my my_sqly query.
everything else is working fine.
Any help would be greatly appreciated.
This is my PHP "process" file (update.php)
if (isset($_POST['button1']))
{
$id = $_POST["id"];
mysql_query("DELETE FROM member WHERE id = '".$id."'") or die("cannot execute the query");
}
and my other file
while($row = mysql_fetch_assoc($results))
{
$id = $row["id"];
//echo '<form action="update.php" method="post">';
echo '<table border="1">';
echo '<tr>';
echo '<td> '.$row["username"].'</td>';
echo '<td> '.$row["password"].' </td>';
echo '<form method="POST" action="update.php">';
echo '<input type="hidden" name="return_url" value="'.$_SESSION["return_url"].'" />';
echo "<input type='hidden' name='hidden_id' value='$id' />";
echo '<input type="submit" name="button1" value="My Button">';
echo '</form>';
}
Change
$id = $_POST["id"];
to
$id = $_POST["hidden_id"];
That is the name of your input field and not only "id"
You are using hidden_id as name for the input where you store $id from previous request.
So to access it, you need to do
$id = $_POST["hidden_id"];
For future reference, take a look at How can I prevent SQL injection in PHP? as you are not sanitizing your user input.
Assume that I have a directory page that is generated by PHP. Each row data is from the database.
echo "<form action=\"reservation.php\" method=\"post\">";
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
if ( stripslashes($row['gymid']) == !0) {
echo "<tr>". PHP_EOL;
echo "<td>".stripslashes($i+1)."</td>". PHP_EOL;
echo "<td align=\"center\">".stripslashes($row['gymname'])."</td>". PHP_EOL;
echo "<td>".stripslashes($row['address'])."</td>". PHP_EOL;
echo "<td align=\"center\">".stripslashes($row['location'])."</td>". PHP_EOL;
echo "<td align=\"center\">".stripslashes($row['operatinghours'])."</td>". PHP_EOL;
echo "<td align=\"center\"><input type= \"submit\" id =\"gym".stripslashes($row['gymid'])."\" value=\"BOOK NOW\" class=\"bookbutton\" onClick=\"reply_click(this.id)\"</td>". PHP_EOL;
//echo "<td align=\"center\"><input type=\"submit\" name=\"gyminfo\" class=\"bookbutton\" value=\"".stripslashes($row['gymid'])."\" >BOOK NOW! </td>". PHP_EOL;
echo "</tr>". PHP_EOL;
}
}
echo "</form>";
So i have a "BOOK NOW" button for each row with unique id (gymxxxx). Upon clicking that button, the page will be redirect to reservation.php where there are a few dropdown boxes with gymname, location, timeslot and so on.
The following shows the dropdown box for gymname:
<label>Gym Name: </label>
<form id="gymInfoForm" method = "post" action = "reservation.php">
<select name="gymSelect"onchange="if (this.selectedIndex) formSubmit();">
<?php
$data = $_POST['gymSelect'];
echo "Data = " . $data;
$query = "SELECT * FROM gymname";
$result = $db->query($query);
$resultNum = $result->num_rows;
echo '<option value="NULL" >Please choose a gym</option>';
for($i = 0; $i < $resultNum; $i++)
{
$row = $result->fetch_assoc();
if($data == $row['gymname'])
{
echo '<option value="' . $row['gymname'] . '" selected>' . $row['gymname'] . '</option>'.PHP_EOL;
}
else
{
echo '<option value="' . $row['gymname'] . '" >' . $row['gymname'] . '</option>'.PHP_EOL;
}
}
?>
</select>
</div>
<div style="margin-bottom:10px">
My question is, how can i post the data (gymname and location) linked to the "BOOK NOW" button to the reservation.php such that the option for "gymname" and "location" will be preselected?
Thank you!
U can do <form> tag for each row or use JS witch map your data and send to the server.
It doesn't look like you have any 'names' for your inputs:
put:
name='bookNow'
in the book now button input tag.
Then put
name='gymName'
in your pre-populated gym name input tag.
Then you can get the values with php:
if(isset($_POST['bookNow'])){
$gymName = $_POST['gymName'];
}
I have following SQL query:
$readNews_SQLselect = "SELECT ";
$readNews_SQLselect .= "live, content, user, created, created_updated, user_updated "; // rows names
$readNews_SQLselect .= "FROM ";
$readNews_SQLselect .= "news "; // table name
$readNews_SQLselect_Query = mysql_query($readNews_SQLselect);
And while loop to display the data from DB:
while ($row = mysql_fetch_array($readNews_SQLselect_Query, MYSQL_ASSOC)) {
$LIVE = $row['live'];
$CONTENT = $row['content'];
$USER = $row['user'];
$CREATED = $row['created'];
$USER_UPDATED = $row['user_updated'];
$CREATED_UPDATED = $row['created_updated'];
echo '<input type="checkbox" value=" '.$LIVE.'" />';
echo '<input value=" '.$CONTENT.'" />';
echo '<p>'.$USER.'<p/>';
echo '<p>'.$CREATED.'<p/>';
echo '<p>'.$USER_UPDATED.'<p/>';
echo '<p>'.$CREATED_UPDATED.'<p/>';
}
mysql_free_result($readNews_SQLselect_Query);
As my echo '<input type="checkbox" value=" '.$LIVE.'" />'; will be either '0' or '1' - how can I convert this string into checkbox checking / unchecking with PHP?
Any suggestion much appreciated.
You'll want to use an In-Line If Statement.
echo '<input id="chkLive" type="checkbox"'.(($LIVE=='1') ? ' checked ' : '').'/>';
I don't know if I understand correctly, but you only want to display which ones are checked, right?
echo '<input type="checkbox" ',($LIVE ? 'checked="checked"':''),'/>';
if $live is your variable which is either 0 or 1,
then you can use:
$boxCheck = '';
if($LIVE == '1')
{
$boxCheck = 'checked="checked"';
}
else
{
$boxCheck = '';
}
echo '<input type="checkbox" value=" '.$LIVE.'" '.$boxCheck.'/>';
$checked = ($LIVE) ? 'checked="checked"' : '';
echo '<input type="checkbox" '.$checked.' value=" '.$LIVE.'" />';