PHP, HTML code review - php

I am working on a project called online Test, where users can take the test. I have created a php code in the following format but its hard coded. Below is the code. If I click on C it will take me to c test and similarly c++
<center><h2><b>Select the test</b></h2>
<div class="test"><b>C</b></div><br>
<div class="test"><b>C++</b></div><br>
</center>
Now the question is I want to make this page has dynamic. To fetch all the course that are there in the database and if user selects a particular test, he should get the test for that particular subject.
<form action="promo.php" method="post">
<h2><center>
<td><select name="links" id="links" value=' ' ">
<?php
while($row=mysql_fetch_array($out))
{
echo "<option value=" .$row['course_id']." > ". $row['c_name']." </option>";
}
?>
</select>
<input type="Submit" value="Start" /></center>
</form>
This is what I have done, it displays all the courses in the drop down fetching from the database. When I click the start button, it does not start any test because promo.php requires another parameter.
I am not able to send the course name along with the promo.php. How to do this
Can any one help me with this code

As some points is not cleared but I assume you can do like this :
Change select box name to name="course"
<select name="course" id="course">
And if you want to send a select box value to a page like this : promo.php?course=c then you should do method = "get" :
<form action="promo.php" method="get">
<h2><center>
<select name="course" id="course">
<?php
while($row=mysql_fetch_array($out))
{
echo '<option value="'.$row['c_name'].'" > '.$row['c_name'].' </option>';
}
?>
</select>
<input type="Submit" value="Start" /></center>
</form>

Perhaps I misunderstood the question, but why not include the other parameter in your form? Just add another <input type="text">, <select> or <input type="hidden">, as appropriate.

Related

Popup on if statement in php

I have a html form which looks like this:
<form action="submitOrder.php" method="get">
<select name="orderForm">
<?php
echo '<option value=" "> </option>';
while($row = \mssql_fetch_array($employeeOrderResult))
{
echo '<option value="'.$row[EMPLOYEE].'">'.$row[EMPLOYEE].'</option>';
}
?>
<option value="Gæst">Gæst</option>
<option value="Praktikant-01">Praktikant-01</option>
<option value="Praktikant-02">Praktikant-02</option>
<option value="Praktikant-03">Praktikant-03</option>
</select>
<br>
Vare: <input type ="text" name="varenr"><br>
Antal: <input type="text" name="antal"><br>
<input type="submit" value="Bestil">
</form>
It fetches som data from a database and adds some special guests.
Now, when it confirms it redirects to a page which has this code in it:
<?php
$ofAntal = $_GET['antal'];
$ofMedarbejder = $_GET['orderForm'];
$ofDato = date('Y-m-d H:i:s');
$ofVareNr = $_GET['varenr'];
$sql = "INSERT INTO Bestillinger(bestillingsAntal,medarbejder,dato,vareNr) VALUES('$ofAntal','$ofMedarbejder','$ofDato','$ofVareNr')";
$validation = mysql_query($sql, $MySQLcon);
if(!$validation)
{
die('Couldnt enter data ' . mysql_error());
}
echo 'Entered data succesfully';
?>
Now, I need a confirmation popup of some kind, if the amount (ofAntal) is above 1, and Ive looked into several solutions. The problem is i started working with PHP tuesday morning, and i cant find a solution that works for me.
All it has to do, is submit the data is yes is clicked, and cancel it if the user clicks no/cancel. This is ofc done in an IF statement, thats not the issue, the issue is how to implement it properly.
ANY help is highly appreciated :)
Use javascript confirm box on onclick attribute for submit button. It will give you yes and cancel options.
<input type="submit" value="Bestil" onclick="confirm("Are you sure ?");">
Orelse
You can write a function in javascript to check that
<input type="submit" value="Bestil" onclick="myfunct();">
function myfunct(){
if(document.getElementById("antal").value > 1)
confirm("Are you sure");
return true;
}

Returning only the last value of query

This is my second code but the problem is I have 3 queries. So it only returns the last product_id when i Click update it always return product_id=3, but i want update the product_id=2
<form action="update_qty.php" method="POST">
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
<?php } ?>
</form>
Your problem is that the PHP is server side and you need something client side to read the value of the text box. You would need a page refresh to pass the text field value to the server so it could write it to the url in the anchor tag. Which is what the form submit would do, but as it would have submitted the value already the anchor tag would be pointless
To do it without a page refresh use Javascript. It would be easy to do with jQuery. You could add an event that writes whatever is entered in the text box the the anchor tags href as it is typed.
I'll do something more like this.
One form per product.In your case when you submit the form the qty value will always be the las found.
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<form action="update_qty.php" method="POST">
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
</form>
<?php } ?>
You can add more information like this
update
You can not get all values as like that because input name overwrite in every loop iteration.
For multiple values you can try in two ways like:
<?php
while($getorder = mysqli_fetch_array($order)){
$newArr[] = $getorder['price']."~".$getorder['product_id']."~". $ getorder['qty'];
} //while end
?>
<input type="hidden" name="allinputs" value="<?=$newArr?>">
Input field outside the loop.
In php explode array value with ~ and get the all values.
Other solution is that
Your input field name must be change like:
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price_<?=$getorder['product_id']?>">
<?php } ?>
Change field name in every iteration.
In current scenario either you need three different buttons or the best solution to use AJAX request .
update
On update_qty.php u can use like this
<?php echo $_GET['product_id'];?>

How do I use PHP to make use of my HTML code

I am doing an assignment for Uni. It requires me to make an HTML form that needs a user to input what their name is, what table number they're sitting on, if they're a regular customer, and their actual coffee order.
I have done this so far, but now I'm asked in the assignment to create a PHP file to make use of this information. I'm not too savvy with PHP and my lecture notes are doing nothing but confusing the hell out of me.
I have a rough idea about GET methods and POST, but how would I do the button coding? The 'show order' button should return a page of the customers' order. I have tried to read plenty of tutorials but this has sadly become my last resort and really hope someone can help me out with proper explanation.
If I'm not clear with anything here please do let me know I will explain it a little more, and no, I'm not asking for anybody to do the code for me, I just need a head start so i know how to start coding it.
My HTML code:
<!doctype html>
<html>
<head>
<title>Task 1 </title>
<style>
</style>
</head>
<body>
<center> <h1>Legendary Ice Creams </h1> </center>
<h4>Customer Information</h4>
<form action"task1.php" method = GET>
Customer Name: <input type="text" name="Your Name" title="Please enter your name here" placeholder="Your name here"><br>
Table number: <select name="Table number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select><br>
Regular Customer: <input type="checkbox" name="regularcustomer"
value="regularcustomer"><br>
<h4> Orders </h4>
<label for = "mulList[]"> Select one or more items</label>
<select id = "mulList[]" name = "mList[]" size = "4" multiple
= "true">
<option value = "First">First</option>
<option value = "Second">Second</option>
<option value = "Third">Third</option>
<option value = "Fourth">Fourth</option>
</select><br>
Select Coffee: <input type="radio" name="coffee">Latte</input>
<input type="radio"
name="coffee">Cappacino</input>
<input type="radio" name="coffee">Flat Half
De-Caf</input><br>
<button type="button" name="showOrder" action="task1.php">Show Order</input>
<button type="button" name="cancelOrder" action="task1.php">Cancel Order</button>
</form>
First of all, your form tag is wrong, the = is missing and the method should be enclosed in quotes too, like this:
<form action="task1.php" method="GET" enctype="multipart/form-data">
Now your actual problem: To give something to another php file, you need to name the HTML element with the name attribute. Then you can call it in this php file with $_GET or $_POST, depending in what you specified in the form tag. So to access your information, in my case just the name, this would be necessary:
$name = $_GET['Your Name'];
Then you can use it however you want. You can also use an easy loop to get all necessary information, the keys will be preserved and the values filtered with filter_var:
foreach($_GET as $k => $v) {
$getvars[$k] = filter_var($v);
}
I hope this covers your actual question, you seemed pretty lost to me, so i tried to explain it as easy as possible.^^
A little sample of an easy form with html/php:
htmlform.html
<html>
<head><meta charset="utf-8"></head>
<body>
<form action="phpfile.php" enctype="multipart/form-data" method="GET">
<input type="text" name="fname">
<input type="text" name="lname">
<input type="submit">
</form>
</body>
</html>
phpfile.php
<?php
$fname = filter_var($_GET['fname']);
$lname = filter_var($_GET['lname']);
echo "Your name is ".$fname." ".$lname;
?>
First of all do not use space inside the name attribute of your HTML elements. Either replace it an underscore or club the two words together.
Inside your task1.php, you can capture the user input by using the $_POST super variable. So $_POST['Your_Name'] will contain whatever the user typed inside the Customer Name textbox. You can have information about all the keys that you need by using:
echo '<pre>';
print_r($_POST); // replace this with $_GET if using method="GET" in your form tag
echo '</pre>';
in your task1.php page. I suggest you start by doing this and take it from there.
Also, remove the action attribute from your <button> tags.
First of all don't use space between names like Your Name in customer name better use Your_Name or anything you want
second add = between action and "task1.php"
and in
<button type="button" name="showOrder" action="task1.php">Show Order</input>
type will be submit i.e
<button type="submit" name="showOrder" action="task1.php">Show Order</input>
task1.php
if(!empty($_GET)){
echo "Customer Name:".$_GET['name'];//Printing Name
...
}
There is a "=" missing.
<form action"task1.php" method = GET>
should be
<form action="task1.php" method = "GET">
right?
Then you should be able to print the selected content in your
task1.php
<?php
echo "<pre>";
print_r($_GET);
echo "</pre>";
?>
Correct?

get selected value of a drop down which is populated with results from an SQL query

So I have a drop down populated with the names based on an SQL query. I want to be able to see which option the user selected before they pressed submit and use this as a variable on a separate php file. I assume I will need to use session variables? I'm a bit lost so any help would be appreciated. I have the following code so far:
<form name="ClientNameForm" id="ClientNameForm" action="ClientDetails.php">
<input type="text" name="ClientName" id="ClientName" placeholder="Type Service User's name here:" style="width: 200px"/><br/><br/>
<select name="Name_dropdown" id="name_dropdown" style="width: 200px" >
<?php
$ClientName_Query= "SELECT CONCAT(FName, ' ', SName) AS FullName FROM ClientDetails";
$ClientName_Result= mysql_query($ClientName_Query) or die (mysql_error());
while ($row= mysql_fetch_array($ClientName_Result)){
echo "<option> $row[FullName] </option>";
}
?>
</select><br/><br/>
<input type="submit" name="submit_btn" id="submit_btn" value="Submit"/>
</form>
In your ClientDetails.php file the value will be available using,
$name = $_POST['Name_dropdown'];
If you need to change a setting in the form document before submitting you can use jQuery. Something like
$('#name_dropdown').change(function(){
var option = $(this.options[this.selectedIndex]).val();
});

GET method with another variable already attached

I need to pass a few GET variables through. The link I need is
index.php?type=door&qty=4
I can write this easily enough like
But I need to select the $qty amount fron a drop down list. So I have
<form name="qty" method="GET" action="../../../index.php?door-type="<?php echo $door_model; ?>">';
<select>
<?php
for ($front_int=1; $front_int<=99; $front_int++)
{
echo'<option value="'.$front_int.'">'. $front_int . '</option>';
}
?>
</select>
<input type="submit" value="front-qty">
</form>
So how can I append these two values into one so that I get then $_GET them on the next page like I would with that first link I created?
If qty needs to be a select, then give that name to the select. Also, pass the other variable with a hidden field, which is pretty standard. Good luck :)
<form name="NOT_QTY" method="GET" action="../../../index.php">
<input type="hidden" name="door-type" value="<?php echo $door_model;?">
<select name="qty">
<?php
for ($front_int=1; $front_int<=99; $front_int++)
{
echo'<option value="'.$front_int.'">'. $front_int . '</option>';
}
?>
</select>
<input type="submit" value="front-qty">
</form>

Categories