PHP form (values disappearing, dont know how it is being processed) - php

I'm a complete newbee in php (started just last week)
Issue is like this:
Basically, I was trying to ensure that once a sub-form is filled, then it is not altered. So, I used !isset to display the sub-form (i.e. if !isset is true) and if !isset is false, then it hides that sub-form and shows the next sub-form (the individuals form has only been designed).
<?php include($_SERVER['DOCUMENT_ROOT'].'/officespace/includes/functions.php');
echo'<html>
<head>
<title> Create </title>
</head>
<body>';
if(!isset($_POST["Category"])){
/* if no category is selected, then this code will display the form to select the category*/
Echo "Pls Select Category before clicking on Submit Category";
/* Breaking out of PHP here, to make the form sticky by using a php code inside form action*/
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Select the Category of Person: </legend><br />
<input type="radio" name="Category" value="Individual" /> Individual<br /><br />
<input type="radio" name="Category" value="Company, Pvt Ltd" /> Company, Pvt Ltd<br /><br />
<input type="radio" name="Category" value="Company, Ltd" /> Company, Ltd<br /><br />
<input type="radio" name="Category" value="Partnership Firm" /> Partnership Firm<br /><br />
<input type="radio" name="Category" value="LLP Firm" /> LLP Firm<br /><br />
<input type="radio" name="Category" value="HUF" /> HUF<br /><br />
<input type="submit" name='Submit Category' value="Submit Category" /><br />
</fieldset>
</form>
<?php
} Else {
$Category = $_POST["Category"];
Echo "$Category";
Echo "<br />";
/* Using swich statement to test the value of Category, and accordingly echo appropriate forms*/
switch ($Category) {
case "Individual":
if(!isset($_POST['Submit_Data_for_Individual'])){
//if no data for individual is submitted,
//then this code will display the form to enter and submit data for Individual
Echo /*displays message*/
"Pls Enter the Data for the Individual";
?>
<form action="<?php Echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<br />
First Namee: <input type="text" name="Individual_First_Name" />
<br />
Middle Name: <input type="text" name="Individual_Middle_Name" />
<br />
Last Name: <input type="text" name="Individual_Last_Name" />
<br />
Date of Birth: <input type="text" name="date_of_birth/incorporation" />
<br />
Gender:
<br />
<input type="radio" name="Gender" value="male" /> Male
<br />
<input type="radio" name="Gender" value="female" /> Female
<br />
Email 1: <input type="text" name="email_1" />
<br />
<input type="submit" name="Submit_Data_for_Individual" value="Submit Data for Individual" />
</fieldset>
</form>
<?php
}Else
{
$email_1 = $_POST["email_1"];
$Gender = $_POST["Gender"];
validate_email($email_1); // this is a custom function which i made
Echo $Gender; // just to see if value has been passes. problem lies here because its not showing anything
// run other validations here
// and if all valid then run mysqli insert query for individuals record
}
break;
case "Company, Pvt Ltd":
echo "Company, Pvt Ltd";
break;
case "Company, Ltd":
echo "Company, Ltd";
break;
case "Company, Ltd":
echo "Company, Ltd";
break;
case "Partnership Firm":
echo "Partnership Firm";
break;
case "LLP Firm":
echo "LLP Firm";
break;
case "HUF":
echo "HUF";
break;
case NULL:
echo "Error: nothing selected";
break;
}
}
echo '</body>
</html>';
?>

Is see one problem immediately.
You are checking for a form input called Submit Data for Individual, but that is the value of a submit button which has no name attribute. Set a name='submit-data' attribute on the submit button and change the conditional to check for the name instead of its value:
// This will never match.
if(!isset($_POST["Submit Data for Individual"])){
// Change it to
if(!isset($_POST["submit-data"])){
// Then change this
<input type="submit" value="Submit Data for Individual" />
// To this:
<input type="submit" name='submit-data' value="Submit Data for Individual" />
Additionally, the default case in a switch statement uses a default keyword:
// You may safely change this:
case NULL:
echo "Error: nothing selected";
break;
// To this:
default:
echo "Error: nothing selected";
break;
Addendum:
The following code is never reachable, since the form posts to another script, create.php. If you change the <form> action attribute to post back to <?php $_SERVER['PHP_SELF'];?> instead of to create.php, you should see the else case. Right now, it doesn't work because your if tests that $_POST["submit-data"] is set. It can only be set if the form has been submitted, but the form submits to an external script.
// This else case can never be reached...
}Else
{
validate_email($_POST["email_1"]); // this is a custom function which i made
Echo $_POST["Gender"]; // just to see if value has been passes. problem lies here because its not showing anything
// run other validations here
// and if all valid then run mysqli insert query for individuals record
}
To fix this and see your Gender echoed out, temporarily change
<form action="create.php" method="post">
// change to
<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
Addendum 2
You are checking if Category is set, but after posting the user form, it will not be:
// Change
if(!isset($_POST["Category"])){
// To check that the user form was not submitted
if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) {
Then you need to test if the user form was submitted. Before the Else { $Category = $_POST['Category']; section, add an else if to process the user form.
if (!isset($_POST["Category"]) && !isset($_POST['submit-data'])) {
// Show the Category form...
}
// Process the user form...
else if (isset($_POST['submit-data'])) {
validate_email($_POST["email_1"]); // this is a custom function which i made
Echo $_POST["Gender"];
}
// Now process the categories or show the user form...
else {
$Category = $_POST['Category'];
// etc...
}
Finally, remove the whole Else block from your individual case, as it cannot be used there.

Related

Retrieving table field when checkbox values are equal to another field

I have language table that include LID,LNAME fields in a UI.
I have many checkboxes for LNAME.
When the user checks some of them and submits, the system display LID for the checked checkbox.
My HTML:
<input type ="checkbox" name="language[]" vaue="English">English
In PHP:
$lang=$_POST['language'];
$sql="select LID from lqnguage where lname=$lang";
Please can anyone help me please I'm having trouble.
Try below code.
PHP
// This is array for language.
$l_array=array();
$l_array[1]='Gujarati';
$l_array[2]='English';
$l_array[3]='Spenish';
// Get selected languages.
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
echo "<pre>";
print_r($_POST['language']);
}
HTML
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<?php foreach($l_array as $lid=>$lname){ ?>
<input type="checkbox" name="language[]" value="<?php echo $lid; ?>" /><?php echo $lname; ?><br />
<?php } ?>
<input type="submit" name="submit" value="Submit" />
</form>
Now, you can get selected language_id(as key) and language_name(as value) in Array.

How do i get a red webpage, i only get yellow (i`m a beginner)

<?php // some of the php code is in dutch
$graden_celcius=0; // dutch for degrees celcius
$html = <<< OET
Hoe warm is het nu? // dutch for " whats the temperature now?"
<br />
<form action="#" method="post">
In graden celcius :
<input type="text" name="dob" value="" />
<br />
<input type="submit" name="submit" value="Tempr" />
</form>
OET;
if(isset($_POST['submit']))
{
if($graden_celcius>=0&&$graden_celcius<=30)
echo '<body style="background-color:yellow">';
else
echo '<body style="background-color:red">';
} else {
echo $html;
}
?>
At this point, add this line
if(isset($_POST['submit'])){
$graden_celcius = $_POST['dob']; //This doesn't filter anything though, in case you are doing further processing.
if($graden_celcius>=0&&$graden_celcius<=30)
You've checked if you have the form sent by the existence of 'submit', but you haven't read the submitted value of the 'dob' POST variable also sent. You can see these variables and what they contain using the Chrome Devtools.
I'm not sure, do you have any other HTML above / below your php though?
You didn't get the value from the post variable. Do this:
if(isset($_POST['submit']))
{
if (isset($_POST['dob']))
$graden_celcius = $_POST['dob'];
if($graden_celcius>=0 && $graden_celcius<=30)
echo '<body style="background-color:yellow">';
else
echo '<body style="background-color:red">';
}
else
{
echo $html;
}
Since you seem to be learning PHP, I'll show you that you could have done this code better, like this:
<?php // some of the php code is in dutch
$graden_celcius=0; // dutch for degrees celcius
if(isset($_POST['submit']))
{
if (isset($_POST['dob']))
$graden_celcius = $_POST['dob'];
if($graden_celcius>=0&&$graden_celcius<=30)
echo '<body style="background-color:yellow">';
else
echo '<body style="background-color:red">';
}
else
{ ?>
Hoe warm is het nu? // dutch for " whats the temperature now?"
<br />
<form action="#" method="post">
In graden celcius :
<input type="text" name="dob" value="" />
<br />
<input type="submit" name="submit" value="Tempr" />
</form>
<?php
}
?>
You can open as many PHP tags as you want in your page. That way you can switch from PHP to HTML easily, and the PHP has control over the HTML output as well.
Your form doesn't supply a number/variable for $graden_celcius. Change your dob input like...
<input type="text" name="graden_celcius" value="" />
<br />
<input type="submit" name="submit" value="Tempr" />
THEN check the value....
if(isset($_POST['submit'])){
$graden_celcius = $_POST['graden_celcius'];
if($graden_celcius>=0&&$graden_celcius<=30)

PHP temperature converter. Resource not found error

I'm trying to create a temperature converter that allows you to input into a text box, check a radio button and convert based on the radio button you check. However when the submit button is pressed it takes me to a page that says "the resource cannot be found" and it says there's an error finding the /converter which is the action of my form. I'm very new to this and couldn't find anything explaining how to fix it
<?php
#$input = $_GET['degrees'];
#$radio = $_GET['celorfar'];
if($radio == 'far'){
$answer = ($input * 1.8) + 32;}
else {
$answer = ($input - 32) * .56; //.56=5/9
}
echo <<<END_OF_FORM
<form method="GET" action="converter">
<input name="degrees" type="text" />
<br />
<input name="celorfar" type="radio" value="far" /> Convert to Fahrenheit<br />
<input name="celorfar" type="radio" value="cel" /> Convert to Celcius<br />
<input name="Convert" type="submit" value="Convert" /> <br />
<br />
Answer : <input name="Text1" type="text" value="$answer" /></form>
END_OF_FORM
?>
You missed file extension...
<form method="GET" action="converter.php">

Add class to link after form is submitted

Im trying to add a class after a form is submitted depending on the option the user chose.
<form enctype="multipart/form-data" action="upload.php" method="POST">
title:<br />
<input type="text" name="title" value="" /> <br />
description:<br />
<input type="text" name="description" value="" /> <br />
Categories:<br />
<input type="checkbox" name="categories[]" value="1">Landscpae<br />
<input type="checkbox" name="categories[]" value="2">Portrait<br />
<input type="checkbox" name="categories[]" value="3">Monochrome<br />
Please choose an image: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
<li class='INSERT OPTION HERE'> test </li>
With the option to select more than one class so add a space after each one.
on my database the Value="1" "2" "3" == the option name
let me know if more information is needed
Something along these lines might work:
$valid = array(1,2,3);#valid items
if(isset($_POST['categories']) && is_array($_POST['categories'])){
$post = array_unique($_POST['categories']);#you might not need this, but this removes duplicate values
foreach($post as $key => $value){
if(in_array($value, $valid)){
$class = true;
}else{
unset($post[$key]);#remove bad values, also this may not be necessary but removes unwanted items.
}
}
if(isset($class)){
$class = 'class="class' . implode(' class', $post) . '"';
}
}#else needed for non array categories?
<li <?php if(isset($class)){ echo $class;} ?> > test</li>
DEMO
You want to make sure that the form is submitted, and if it is then you echo a class and can create a switch statement that will alter the class depending on what was selected.
<li
<?php
if(isset($_POST['categories'])) {
echo 'class="';
switch($_POST['categories']) {
case 1;
echo 'class1';
break;
case 2;
echo 'class2';
break;
case 3;
echo 'class3';
break;
echo '"';
}
}
?>
> test </li>

PHP Sessions + Radio buttons to link to a specific form

Firstly here is my PHP CODE
if(!isset($_POST['selection'])){
$missing['selection'] = $required['selection'];
}
if(empty($missing)) {
post2session();
$_SESSION['step'][0] = 0;
redirect("");
}
Here is my HTML
<form action="" method="post">
<table cellpadding="0" cellspacing="0" border="0" class="tbl_insert">
<tr>
<th><label for="selection">Select from following that applies to you</label></th>
<td>
<input type="radio" name="selection" id="selection" group="form_type" value="form1"> />Form 1<br />
<input type="radio" name="selection" id="selection" group="form_type" value="form2" />Form 2<br />
<input type="radio" name="selection" id="selection" group="form_type" value="form3" />Form 3<br />
<input type="radio" name="selection" id="selection" group="form_type" value="form4" />Form 4<br />
</td>
</tr>
</table>
</form>
How would i redirect the user to FORM1 if they selected radio "form1"; FORM2 if they selected "form2"; etc.
I appreciate the help you will provide (Y)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
switch($_POST['selection']) {
case 'form1': $url = '/wherever/form1/is'; break;
case 'form2': $url = '/wherever/this/other/form/goes'; break;
...
default: $url = '/some/default/handler';
}
redirect($url);
}
Redirects to new pages are normally done like this:
header('Location: www.site.tld/anotherpage.php');
exit();
first turn name attribute into array name="selection[]"dio
and also do not use same id for each radio button, either u r able to check all radio button together,because php works with name and javascript works wth id
and on post page
try to check via print_r($_POST['name'])

Categories