Get data from a post form - php

I don't understand why I can't use my last form in this code. I generated a form using a SELECT list to select the member that I want to update and it works, but I don't know why I can't use datas from this form. Actually, I can't even echo something (see the echo "TEST"; at the end, nothing happens when I submit the form).
<?php $mysqli = new Mysqli("localhost", "root", "", "repertoire"); ?>
<form method="post" action="">
<label>Modifier</label>
<select name='id_modif'>
<?php
$resultat = $mysqli->query("SELECT * FROM annuaire");
while($select = $resultat->fetch_assoc()){
echo "<option value=". $select['id_annuaire'] . ">" . $select['prenom'] . " " . $select['nom'] . "</option>";
}
?>
</select>
<input type ="submit" name="modifier">
</form>
<br>
<?php
if (isset($_POST['modifier'])){
//print_r($_POST);
$resultat = $mysqli->query("SELECT * FROM annuaire WHERE id_annuaire = '$_POST[id_modif]'");
while ($modif = $resultat->fetch_assoc()) {
echo '<form method="post" action="">
<label for="nom">Nom *</label><br>
<input type="text" name="nom" value="' . $modif['nom'] . '"> <br>';
echo '<label for="prenom">prenom *</label><br>
<input type="text" name="prenom" value="' . $modif['prenom'] . '"> <br>';
echo '<label for="telephone">telephone *</label><br>
<input type="text" name="telephone" value="' . $modif['telephone'] . '"> <br>';
echo '<label for="profession">profession *</label><br>
<input type="text" name="profession" value="' . $modif['profession'] . '"> <br>';
echo '<label for="ville">ville *</label><br>
<input type="text" name="ville" value="' . $modif['ville'] . '"> <br>';
echo '<label for="codepostal">codepostal *</label><br>
<input type="text" name="codepostal" value="' . $modif['codepostal'] . '"> <br>';
echo '<label for="adresse">adresse *</label><br>
<textarea name="adresse">' . $modif['adresse'] . '</textarea> <br>';
echo '<label for="date_de_naissance">Date de naissance</label><br>
<input type="date" name="date_de_naissance" value="' . $modif['date_de_naissance'] . '"><br>';
echo '<label for="sexe">sexe</label><br>
<input type="radio" name="sexe" class="sexe" value="m" checked>Homme
<input type="radio" name="sexe" classe="sexe" value="f">Femme<br>';
echo '<label for="description">description *</label><br>
<textarea name="description">' . $modif['description'] . '</textarea> <br>';
echo '<input type="submit" name="valider_modif" value="Modifier"> <br>';
}
if (isset($_POST['valider_modif'])){
echo "TEST";
}
}
?>

Your second if check is inside the other one, so it will only run when both $_POST['modifier'] and $_POST['valider_modif'] are set. But you second form does not send modifier anywhere.
You could add a hidden field to your second form:
<input type="hidden" name="modifier" value="1" />
Or if you don't want to show the second form again, move the second if outside the other.
Also, you should not use $_POST values in SQL queries directly to be safe from SQL injection. A function like mysqli_real_escape_string should be used to escape the value first.

You have 2 forms and you are not closing the 2nd with </form>

Took me a long time to go through your code. Okay. First thing. Try not to echo so much html markup. It just makes you code the clunkiest in the world. From what I gathered, the Modifier button comes up when you click on the first button. What you need to do if you want to see the TEST message is to take it out of the if statement because the Buttons are like an XOR gate. Setting the other unsets the other

Related

Select only shows last name

I have a form where you need to be able to select the mentor, and add a name. The selecting a mentor is working fine, apart from one part: It only shows the last name ('achternaam') and it needs to show the first name ('voornaam') too. What did I do wrong?
<form action="index.php" method="post">
<p>
<label for="mentor">Mentor:</label>
<select name="mentor" id="mentor">
<?php
if ($result->num_rows > 0) {
//output data of each row
while ($mentors = $result->fetch_assoc()) {
echo '<option value="' . $$mentors['voornaam'] . '">' . $mentors['achternaam'] . '</option>';
}
} else {
echo "0 results";
}
?>
</select> <br>
<label for="klasnaam">Klasnaam:</label>
<input type="text" id="klasnaam" name="klasnaam"> <br>
<input type="hidden" name="action" value="addClass" />
<input type="submit" value="Submit">
<?php
?>
</p>
</form>
The <option> element displays what is written between the opening and closing tag. In your current version you only write the last name between the tags.
To fix this, add the first name:
echo '<option value="' . $mentors['voornaam'] . '">' . $mentors['voornaam'] . ' ' . $mentors['achternaam'] . '</option>';
The value attribute is not used for displaying. It is the representation of the data when submitting the form.
Note that I also changed $$mentors['voornaam'] to $mentors['voornaam']. There was a $ too much.

PHP/HTML to populate a text input with a value from database on load, but write the new value to DB without appearing in URL?

I have two little bits of code doing the work here:
HTML:
<form action="http://mypage.php">
<input type="text" name="AnswerArea" ID="AnswerArea" value="' . $this_answer . '" />
<input type="hidden" name="Q" value="' . $NextQ . '" />
<input type="hidden" name="P" value="' . $quest . '" />
<input type="hidden" name="ref" value="' . $ref . '" /><br><br>
<input class="button" name="submit" type="submit" value="Next" />
</form>
And the PHP:
<?php if ($AStyle != 'None'){
if(array_key_exists('AnswerArea', $_GET)) {
include 'connection.php';
$newanswer = $_GET['AnswerArea'];
$newSQL = 'UPDATE answers SET Q' . $prev . '="' . $newanswer . '" WHERE strRef="' . $ref . '"';
if (mysqli_query($mysqli, $newSQL)) {
echo 'Successful, comment out when ready';
}
mysqli_close($mysqli);
}
}
?>
The intention is that when submit is clicked the Q, P & ref values are in the URL and the new value of $this_answer is written to the database. However, the input submit and AnswerArea variables are appearing in there as well. I know you can't have GET and POST in the same form, so how should I go about doing this?
The AnswerArea variable could be longer than the permissible URL length so I want to avoid it getting passed as an address.
Any help gratefully received!

PHP $_POST getting a 404 error

I am trying to practice my php form interaction and I ran into a little snag. This code works fine when I take out the month and the day selections however when I leave them in I get a 404 Page Not Found Error.
<?PHP
if ($_POST['Submit']){ // if the form was submitted
echo $_POST['homeTeam'];
echo $_POST['awayTeam'];
echo $_POST['matchDate'] . ' ';
echo $_POST['month'] . ' ';
echo $_POST['day'] . ', ';
echo $_POST['year'];
}
else { //if the form hasn't been submitted yet
?>
<form action="" method="post" id="addMatch">
<label id="homeTeam">Home: <input type="text" name="homeTeam" /></label>
<label id="awayTeam">Away: <input type="text" name="awayTeam" /></label>
<label id="month">Month:
<select name="month">
<?php foreach(range(1,12) as $month){
echo '<option value="' . $month . '">' . date("F",strtotime("0000-$month")) . '</option>';
}
?>
</select></label>
<label id="day">Day:
<select name="day">
<?php foreach(range(1,31) as $day){
echo '<option value="' . $day . '">' . $day . '</option>';
}
?>
</select></label>
<input type="hidden" name="year" value="2014" />
<input type="submit" name="Submit" />
</form>
<?php
}
?>
I've tried rewriting the code a couple of different ways and always the same result.

Display different row of a database upon clicking a button

I have a database of mosques with the following fields: mosque_id, mosque_name, prefecture, address, postal_code, website, email1, phone1 . What I want to do is to display the information of the first mosque (mosque_id=1) in a form and when I click "Next", the information of the second mosque (mosque_id=2) will be displayed.
So far, I've been able to use mosque_id to select an entire row and display the information, but I've been struggling to find a way to switch to the next one. I'm very new to PHP and MYSQL, any help would be appreciated.
<?
$mosque_id = 1;
$result = mysql_query("SELECT * FROM Mosque WHERE mosque_id='$mosque_id'");
$row = mysql_fetch_assoc($result);
?>
<form action="insert.php" method="post">
Mosque Name: <?php print('<input type="text" name="firstname" value="' . $row['mosque_name'] . '"/><br>')?>
Prefecture: <?php print('<input type="text" name="firstname" value="' . $row['prefecture'] . '"/><br>')?>
Address: <?php print('<input type="text" value="' . $row['address'] . '"/><br>')?>
Postal Code: <?php print('<input type="text" value="' . $row['postal_code'] . '"/><br>')?>
Website: <?php print('<input type="text" value="' . $row['website'] . '"/><br>')?>
Email 1: <?php print('<input type="text" value="' . $row['email1'] . '"/><br>')?>
Contact number 1: <?php print('<input type="text" value="' . $row['phone1'] . '"/><br>')?>
<input type="button" value="Next Mosque" onclick=""/>
</form>
<?php
$mosque_id = (isset($_GET['mosque_id']) ? intval($_GET['mosque_id']) : 1); // suppose you know that '1' is the first id in your table
$result = mysql_query("SELECT * FROM Mosque WHERE mosque_id>='$mosque_id' limit 2");
$row = mysql_fetch_assoc($result); // this is the row you're gonna display in the form
$row2 = mysql_fetch_assoc($result); // this will tell us about the next row
?>
<form action="insert.php" method="post">
Mosque Name: <?php print('<input type="text" name="firstname" value="' . $row['mosque_name'] . '"/><br>')?>
Prefecture: <?php print('<input type="text" name="firstname" value="' . $row['prefecture'] . '"/><br>')?>
Address: <?php print('<input type="text" value="' . $row['address'] . '"/><br>')?>
Postal Code: <?php print('<input type="text" value="' . $row['postal_code'] . '"/><br>')?>
Website: <?php print('<input type="text" value="' . $row['website'] . '"/><br>')?>
Email 1: <?php print('<input type="text" value="' . $row['email1'] . '"/><br>')?>
Contact number 1: <?php print('<input type="text" value="' . $row['phone1'] . '"/><br>')?>
<input type="button" value="Next Mosque" onclick="window.location = 'path-to-your-page?mosque_id=<?php echo $row2['mosque_id'];"/>
</form>
First: please use mysqli functions or PDO class, not mysql functions, they are deprecated.
Secondly: use long PHP tags
But You are on right track:
Replace Your first row with:
$mosque_id = (isset($_GET['mosque_id']) ? intval($_GET['mosque_id']) : 1);
And in the end
<input type="button" value="Next Mosque" onclick="javascript: document.location = '?mosque_id=<?php echo ($mosque_id + 1);?>"/>
You also need to write some checks after query, if such id exists and actually, if you develop further, you need to check for next mosque_id also, because if you delete some rows, id-s may not be in correct order like 1, 2, 3, 4 but 1, 3, 7, 9
(hint: for next id use WHERE mosque_id > current_id ORDER BY mosque_id DESC LIMIT 1 in MySQL)
Just have a hidden input holding the ID, and increment it when the page loads. Then, when you submit, the ID will be submitted too.
<?
if (isset($_POST['id']) {
$id = $_POST['id'];
}
else {
$id = 1;
}
$result = mysql_query("SELECT * FROM Mosque WHERE mosque_id='$id'");
...
?>
<form action="insert.php" method="post">
...
<input type='hidden' name='id' value='<?php echo $mosque_id++; ?> />
<input value="Next Mosque" />
</form>
And by the way, use PDO. It's very easy to learn and adds a huge level of security to your application.

Populating and creating two dropdown list from array using PHP

I am encountering a strange problem when trying to create two dropdowns where the values are loaded from two different arrays. The problem is I can see only one dropdown and at the end of the list I see the label of the next dropdown, finally it is also not showing any submit button which I have created after the dropdown lists.
Let me make it clear with some code and screenshots, so that one can have an idea:
if($output_form == 1){
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>ID</label>
<input type="text" name="ID" /><br /><br/>
<label>Name</label>
<input type="text" name="name" /><br /><br/>
<label>Upload1:</label>
<input type="file" name="upload1" /><br />
<label>Upload2:</label>
<input type="file" name="upload2" /><br />
<label>Catalogue</label>
<select name="cataloguepdfs" />
<?php
foreach ($catalogueArray as $cataloguePDFName) {
echo '<option value="'. $cataloguePDFName . '">' . $cataloguePDFName . '<option />';
}
?>
<label>Template</label>
<select name="templatepdfs" />
<?php
foreach ($templateArray as $templatePDFName) {
echo '<option value="'. $templatePDFName . '">' . $templatePDFName . '<option />';
}
?>
<input type="submit" name="submit" value="Submit">
</body>
</html>
<?php
}
?>
Here is the screenshot for the above code. Please note that the rest of my code is working good, fetching the .pdf file names from the respective arrays. Please note that my pdfs are named 1.pdf, 2.pdf and so on
On the other hand if I move the template dropdown list above the catalogue dropdown list it is the other way round with 'Catalogue' as the end element in the dropdown list.
If there is only dropdown list everything works fine including the presence of the submit button.
Is there a way how I can display both the dropdowns after populating them from respective arrays. Is there some problem with my code. Any help would be appreciated.
Thanks
You need to close select after options
<select name="cataloguepdfs">
<?php
foreach ($catalogueArray as $cataloguePDFName) {
echo '<option value="'. $cataloguePDFName . '">' . $cataloguePDFName . '<option />';
}
?>
</select>
You must close the tag after options.
Something like this
<label>Catalogue</label>
<select name="cataloguepdfs">
<?php
foreach ($catalogueArray as $cataloguePDFName) {
echo '<option value="'. $cataloguePDFName . '">' . $cataloguePDFName . '<option />';
}
?>
</select>
<label>Template</label>
<select name="templatepdfs">
<?php
foreach ($templateArray as $templatePDFName) {
echo '<option value="'. $templatePDFName . '">' . $templatePDFName . '<option />';
}
?>
</select>

Categories