Why is my Post method not working? - php

Sorry in advance if this is a really stupid mistake, but I appreciate any help that can be given.
This is the code that initialise that data in the form. They are radio buttons that are populated via an SQL database.
echo "<form method='post' action='home.php'>";
$result = queryMySQL("SELECT Name FROM Quotees");
if ($result->num_rows == 0) {
$error = "<span class='error'>No people can be found</span><br><br>";
} else {
$numrow = $result->num_rows;
for ($count = 1; $count <= $numrow; $count++) {
$row = $result->fetch_array(MYSQLI_ASSOC);
$name = stripslashes($row['Name']);
echo '<input type="radio" name="choice" value="' . $name . '" />' . $name;
echo '<br>';
}
}
Later on in the code I try to access the value picked using the POST method, as follows:
$personPicked = stripslashes($_POST["choice"]);
if ($person === $personPicked) {
//code
} else {
echo "wrong answer";
}
The error I receive is "Undefined index: choice" on the line where the POST method is called.
I submit the form using the following code out side of the php segment as HTML code
<div class="confirmButton">
<form action="home.php">
<input type="submit" value="Confirm">
</form>
Thanks for any help you can give as to why my code isn't working/how I can go about solving the problem.

The submit button is in a different form.
When you click it, you submit the form that does not include the radio buttons.
You need to put the submit button and the radio buttons in the same form.

Related

Populate dropdown based on checkbox

I have a form that contains a checkbox and a dropdown list. I use PHP to populate the dropdown list from two MySQL tables. Which table I use depends on whether the checkbox was checked or not. But, as you can see from my code sample, I can only populate that dropdown list after I submit the form. I want to be able to keep clicking on the checkbox and keep re-populating the dropdown list without having to submit the form. I am still new to this, so, please, give me as simple a solution as possible.
PS. I have a small bug in the code that doesn't keep the checkbox checked/unchecked after I submit the form. Working on fixing it.
Thank you
<?php
$Prc_Earn = 'Prc';
if (isset($_POST['submitBtn']))
{
if (isset($_POST['chkbox']))
{
$sql = 'select distinct Symbol from price_history.quarterly_earnings_history';
$Prc_Earn = 'Earn';
}
else
{
$sql = 'select distinct Symbol from price_history.stock_symbol_list';
$Prc_Earn = 'Prc';
}
}
// Connect to the database
include ('../Connections/mysql_connect.php');
if ($dbcon)
{
?>
<form method='post'>
<label for=''>Earnings / Prices</label>
<input type='checkbox' name='chkbox' id='chkbox' <?php if ($Prc_Earn == 'Prc') { echo "checked"; };?>><br><br>
<label for='symbol'>Stock Symbol:</label>
<select name = 'symbol' id='symbol'>
<?php
$result = mysqli_query($dbcon, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{ echo '<option value="' . $row['Symbol'] . '">' . $row['Symbol'] . '</option>'; }
}
?>
</select><br><br>
<button class='button' type='submit' name='submitBtn' id='submitBtn'>View Data</button><br><br>
</form>
<?php
}
?>

multi if and elseif statement in form

I have a form on a single page, if it don't have a id I want it to insert. but if there is an id then update.
I am having difficulties getting the form values to display in the button.
I am also not sure my if or else if statement is plausible.
<?php
function myform(){
if(isset($_POST['add'])) {
if ($myform_id > 0) {
//my insert query here
$btn_value = "add";
$btn_name = "add";
}elseif(isset($_POST['edit'])){
//my update query here
$btn_value = "edit";
$btn_name = "edit";
}
}
}
?>
My form button
<input type="submit" name="<? $btn_name?>" value="<? $btn_value?>">
try this
<input type="submit" name="<?echo $btn_name;?>" value="<? echo $btn_value;?>">
within the php tag itself give the following statement
print "<input type='submit' name='$btn_name' value='$btn_value'>";

PHP While Loop logic

Can you Help me?? I am trying for some validation in my system, but I got stuck up on my ideas.
I created a text box that will looped when you enter any number. When the text box appear the user will have to fill up names, but if the user accidentally type a duplicated name the prompt message will appear saying: "Errors".
There is minor bug in my system and I want to fix it. I have think a lot of methods but this is the best fit to my system.
Here is the code:
<form action="fq1.php" method="post">
<input type="text" name="num" id="num">
<input type="submit" value="select" name="select" id="select" />
<?php include 'conectthis.php';
$num = $_POST['num'];
if(isset($_POST['select']))
{
$x = 0;
while($x <= $num)
{
$x++;
echo "<input type='text' name='txt".$x."' />";
}
echo "<input type='submit' name='save' id='save' value='Save'>";
}
if(isset($_POST['save']))
{
$y = 0;
while($y<=$num)
{
$y++;
$mypost1 = "txt". $y;
if($_POST[$mypost1] == $_POST[$mypost1])
{
$prompts = "<script>alert('Names was not saved, please ensure that there are no duplicated Names');</script>";
}
else
{
$runthis = "Update sampletable SET samplename = '$_POST[$mypost1]' WHERE id = '$y'";
mysql_query($runthis) or die(mysql_error());
}
}
echo $prompts;
}
?>
</form>
Now my problem is if($_POST[$mypost1] == $_POST[$mypost1]) the textbox will recognize it self and show the prompt message even there is no duplicated name(s).
It's a scratch from my code I think I know it is right.
Thank you in advance, I am new to this and still studying.

PHP variable returns a null value

I've created a textbox so when the admin types a name and clicks submit, it will shows a list of retrieved data from the database.
<form method="post" action="">
<?php
$teacherName = $_POST['teacherName'];
if ($_POST['submitted'] == 1) {
if($teacherName != ""){
$getName = mysql_query("SELECT name, user_id FROM members WHERE name = '$teacherName'");
$teacherdetails = mysql_fetch_array($getName);
$teachername = $teacherdetails['name'];
$teacher_id = $teacherdetails['user_id'];
if($teachername != ""){
print $teachername . "<br/>";
} else {
print "Give a valid name <br/>";
}
}
}
if ($teachername == ""){ ?>
Teacher name:<input type="text" size="20" name="teacherName"><input type="hidden" name="submitted" value="1"><br/>
<input type="submit" value="Submit" />
<?php $getModule = mysql_query("......");
while ($row2 = mysql_fetch_array($getModule)) { ?>
<input type="checkbox" name="modules[]" value="<?php print $row2["module_id"]?>"/> <?php print $row2["module_name"] . '<br/>'; } ?>
</div><br/> <?php } ?>
<input type="submit" value="Submit" />
</form>
Below I wrote this code (in the same script):
<?php
$modules = $_POST['modules'];
for($i = 0; $i < count($modules); $i++){
$module=mysql_query("INSERT INTO module (module_id,user_id) VALUES ('$modules[$i]','$teacher_id')");
}
?>
but for some reason when I call the variable "$teacher_id" (which is the value I retrieved before from the database. It works fine in the form) it returns nothing. It's null but I can't understand why.
Am I doing something wrong?
First off, put the PHP outside the form tags, at the top.
Secondly, the data you are receiving could be an array; with more than one result set;
do this just incase it it returned as that;
foreach($teacherdetails AS $teacher) {
//also set the values of the variables here
echo $teacher['name'];
echo $teacher['user_id'];
}
Regarding the last bit, is the $teacher_id successfully printing a result?
Also, where is the modules being input and posted from?

HTML/PHP Survey not passing to MySQL database properly

I'm trying to make a small survey that populates the selections for the dropdown menu from a list of names from a database. The survey does this properly. I want to submit the quote the user submits with this name into a quote database. The quote text they enter into the field goes in properly, however, the name selected from the menu does not get passed in. Instead I get a blank name field.
I understand some of my code is out of context, but the name is the only thing that does not get passed in properly.
On form submit, I include the php file that submits this data to the database:
<form action="<?php $name = $_POST['name']; include "formsubmit.php";?>" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<input type="submit" value="Submit!" />
</form>
The variable $name comes from this (which populates my dropdown menu):
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
echo "</select>";
And here is my formsubmit.php:
<?php:
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
header("Location: quotes.php");
if (#mysql_query($sql)) {
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
Your form action stuff looks weird, but regardless, I think the problem you're having has to do with not setting $name = $_POST['name'] like you're doing with $quotetext = $_POST['quotetext']. Do that before the sql statement and it should be good to go.
edit to try to help you further, I'll include what the overall structure of your code should be, and you should tweak it to fit your actual code (whatever you're leaving out, such as setting $query for your name options):
file 1:
<form action="formsubmit.php" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<select name='name'>
<?php
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
?>
</select>
<input type="submit" value="Submit!" />
</form>
formsubmit.php:
<?php
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$name = $_POST['name'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
$nyme = htmlspecialchars($temp['name']);
echo "<option value='$nyme'>$nyme</option>";
}
echo "</select>";-
This way you will receive the value of the name in $_POST array
and you have to get that value out of $_POST array as well you need to change the
code add the following line to get the name in your script.
$name = $_POST['name'];
you need to change the form action tag
<form action='formsubmit.php' .....>
and in that file after successful insertion you can redirect the user to whereever.php.
so it was fun explaining you every thing bit by bit change this now in your code as well.
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}

Categories