PHP $_POST getting a 404 error - php

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.

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.

Get data from a post form

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

Not receiving data from $_POST

I have this
<select name="method">
<?php
while($m = $sel->fetch(PDO::FETCH_ASSOC)){
echo '<option value="' . $m['type'] . '">' . $m['name'] . '</option>';
}
?>
</select>
And in the html form, in the source, it shows the type and name, and everything works, but when I submit the form, $_POST['method'] is blank, and var dumped is NULL. Would be great if someone could help.
Umm your data isn't inside of a form?
<form method="post">
<select name="method">
<?php
while($m = $sel->fetch(PDO::FETCH_ASSOC)){
echo '<option value="' . $m['type'] . '">' . $m['name'] . '</option>';
}
?>
</select>
</form>
<form name="input" action="your path" method="post">
<select name="method">
<?php
while($m = $sel->fetch(PDO::FETCH_ASSOC)){
echo '<option value="' . $m['type'] . '">' . $m['name'] . '</option>';
}
?>
</select>
</form>

Retain dropdown list(dynamic not hard coded) value after submitting the form php

I need to retain the dropdown list value after post submit where the values for the dropdown list are read populated from an array, not hardcoded.
This is my code for populating the dropdown list:
<?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">
Can somebody guide me how can I echo the selected dropdown value. I have seen examples how it can be done when the values are hardcoded, but for some reason im running into errors when trying for my dynamic dropdown list. Any help would be appreciated.
Thanks
Did you mean something like this?
Put your code in :
if(array_key_exists('submit' , $_POST))
{
echo $_POST['templatepdfs'];
}
else
{
?>
<form method='post' action=''>
<--Your code here-->
</form>
<?php
}
OR:
<form method='post' action=''>
<label>Template</label>
<select name="templatepdfs" />
<?php
foreach ($templateArray as $templatePDFName) {
echo '<option value="'. $templatePDFName . '"';
if($templatePDFName == $_POST['templatepdfs']) echo ' SELECTED';
echo '>' . $templatePDFName . '<option />';
}
?>
<input type="submit" name="submit" value="Submit">
</form>

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