I'm starting with php (but I was quite experienced on C++) and I have some questions. The thing is that I would like to have several forms in the index.php, and to have the code more clean I would like to call functions to call the forms.
Right now, I'm having the running code as teh follow:
<html>
<head>
<title>Prueba de PHP</title>
</head>
<body>
<form id="nomfromnum" action="getCharName.php" method="GET">
<input type="number" min="1" name="charidbox" required>
<input type="submit" value="Seleccionar personaje">
</form>
<form id="despPers" method="POST">
<select name="nomChar">
<?php
include("conection.php");
$con=conectar();
$sql = "SELECT * FROM Personajes";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["Character_id"].'">'.$row["Character_name"].'</option>';
}
?>
</select><br>
</form>
</body>
</html>
In the first form, I was able to move it into another file (getCharName.php). But for the second function (despPers), that creates a dropdown menu and populates it with its values, I wan't able to find the way to move it to another file. I tried to chance the header for and create a file despPers.php with the following code but it didn't worked.
<select name="nomChar">
<?php
function despChar($conexion){
$sql = "SELECT * FROM Personajes";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["Character_id"].'">'.$row["Character_name"].'</option>';
}
}
include("conection.php");
$con=conectar();
despChar($con);
$con->close();
?>
</select><br>
Can anybody guide me with this? Maybe it's something basic, but I'm quite freshman on php and html.
Thanks a lot!
Oops, it was a wrong variable name.
Anyway, here you have what I have done.
On the index.php
<form id="despPers" action="despChar.php" method="POST">
<select name="nomChar">
<?php
include("despChar.php");
despChar();
?>
</select><br>
</form>
And the despChar.php is
<?php
function despChar(){
include("conection.php");
echo '<option value="0">Character</option>';
$con=conectar();
$sql = "SELECT * FROM Personajes";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row["Character_id"].'">'.$row["Character_name"].'</option>';
}
$con->close();
}
?>
Related
I want the dropdown to show the "client_code", "name" in one line. It almost works but not 100%. I am a beginner with php and SQL, can someone help me please?
Code that doesn't work
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$queryKlantCode = "SELECT klant_code FROM klant";
$resultKlant=mysqli_query($mysqli,$queryKlant);
$resultKlantCode=mysqli_query($mysqli,$queryKlantCode);
while($row=mysqli_fetch_array($resultKlant) &&
$row2=mysqli_fetch_array($resultKlantCode) )
{
?>
<option><?php echo $row[0]. ", ". $row2[0];?></option>
<?php
}
?>
</select>
</form>
Code that only works with retrieving name in dropdown from database
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$res=mysqli_query($mysqli,$queryKlant);
while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row[0]; ?></option>
<?php
}
?>
</select>
</form>
You can select more than one column from a table in the same select, and as both these columns live in the same table it makes producing this result much simpler.
<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$sql = "SELECT naam, klant_code FROM klant";
$result = mysqli_query($mysqli,$sql);
while($row=mysqli_fetch_array($result)){
?>
<option><?php echo $row[0]. ", ". $row[1];?></option>
<?php
}
?>
</select>
</form>
You probably want to do this with your <option> tag as well rather than put the name and code in the visible portion
<option value="<?php echo $row[1];?>"><?php echo $row[0];?> </option>
And if you use mysqli_fetch_assoc() you can use the columns names so you know what you are putting where
while($row=mysqli_fetch_assoc($result){
<option value="<?php echo $row['klant_code'];?>"><?php echo $row['naam'];?> </option>
Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="POST">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit">
</form>
</div>
</div>
</body>
</html>
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label
for='.$question_body.'>Yes</label>
<input type="checkbox" name="'.$option_a.'" value="'.$option_a.'">
<input type="hidden" name="'.$question_id.'" value="'.$question_id.'">
<hr>
';
}
}
}
?>
Answer.php
<?php
include 'conn.php';
if(isset($_POST['Submit'])){
if(isset($_POST['answer'])){
print_r($_POST);
}
}
?>
The value checkbox and hidden input did not send any value on the Answer.php page. It did not send any error or warning. 'Option_a' == 'Yes' value and 'question_id' == S1,S2,S3 and so on. Each 'question_id' have their own question and user have to tick if yes. So i want send these value on another page. I hope any of you guys can help me.
I can understand what you actually want to do but not sure what you are actually trying to achieve by saving the options as name and question_id as hidden input.
How i would approach this is saving the question_id as name for the options and i would suggest to use radial buttons if its an yes or no question. Anyways to solve your problem make the following changes to your question format in pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label>'.$question_body.'</label>
<input type="checkbox" name="'.$question_id.'" value="'.$option_a.'">
<input type="checkbox" name="'.$question_id.'" value="'.$option_b.'">
<hr>
';
}
}
}
?>
Now here each question having two options will have the same name corresponding to the question and values as the options. Now your Answer.php should look like
<?php
include 'conn.php';
if(isset($_POST['Submit'])){ // if submit is true
// if you want to print all answers then loop through the question id
// $_POST[$question_id] gives you the value of the answer
//i.e the option the user has chosen for that particular question
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
echo $_POST[$question_id];
}
}
}
?>
Hope this makes sense and helps. :)
check here for more info on checkboxes.
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'])){
I want to print the name and last name of an ID entered in the text box. Here is the PHP and HTML code:
<head>
<title>
Search your name by ID
</title>
</head>
<?php
if(isset($_POST["searchname"]))
{
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
$a = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($a);
echo "$row[0] , $row[1] , $row[2]";
}
else
{
echo "error";
}
?>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
Output when I enter ID:
, ,
There are entries in the MySQL table but I am unable to fetch them. What is wrong with my code?
UPDATE: I have also tried mysql_fetch_array but it is not working.
Main problem is that you're miximg mysqli and mysql. These are absolutely different APIs.
Assuming you have
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
Next you should:
$result = $connect->query("Select * from users where id='$id'");
Then get results:
while ($row = $result->fetch_assoc()) {
var_dump($row);
}
And of course, instead of directly putting values into your query use prepared statements.
Update:
about mistakes:
Your main mistake is mixing apis. When you use mysql (which is deprecated and you mustn't use it anymore) you can't use any of mysqli functions and vice versa.
Next - as you create mysqli object with new, you should work in object-oriented style, i.e. calling methods from your mysqli object.
Try this:
<html>
<head>
<title>
Search your name by ID
</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST["searchname"])){
$id = $_POST["searchname"];
$connect = mysql_connect("localhost","adarsh","Yeah!","adarsh");
$result = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($result);
print_R($row);
}else{
echo "there is something wrong";
}
I have a simple dropdown menu which posts the result to itself but when i choose one of the options in the drop down menu it does not echo back the result as expected.
I'm sure i've just missed out something simple but can't spot it. Any ideas? The form posts but does not echo back $user_settings.
<?php
include "functions.php";
connect();
$sql="SELECT user_id, user_realname FROM users ORDER BY user_realname ASC";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$name=$row['user_realname'];
$options.="<OPTION VALUE=>".$name.'</option>';
}
if(isset($_POST['submit'])){
$user_realname = $_POST['username_select'];
$user_select = mysql_query("SELECT user_id, user_realname FROM users WHERE user_realname = '$user_realname'")
or die ("Could not get user data");
while($row = mysql_fetch_array($user_select)){
$user_settings = $row['user_id'];
echo $user_settings;
}
}
?>
<html>
<head>
<body>
<form action="<?php echo $PHP_SELF;?>" method="POST">
<tr><label>Choose User to Edit</tr>
<tr><SELECT NAME="username_select"><OPTION VALUE=""></option>User's Name<?php echo $options;?></SELECT></label></tr>
<tr><input type="submit" value="submit" name="submit"></tr>
</form>
<?php echo $user_settings;?>
<br/>
Go Back
</body>
</head>
</html>
User $_SERVER['PHP_SELF'] instead of $PHP_SELF