How to input another value with looping statement in PHP? - php

Hello I am new to programming and I am having a hard time with this. Can someone please give me a tip or help with my code?
The user will be asked if they want to input another value and if they click the "yes button", a text field will show wherein the user will input another value. Or the button can redirect them to the process page again to input another value, something like that. It must be done using looping statement. I tried do while loop but it doesn't seem to work or maybe I am doing wrong. Any help or tip is appreciated.
This is my code on button:
<?php
if(isset($_POST['f-value'])):
$total=($_POST['f-value']-32)*5/9;
echo round($total, 3) . " celcius";
endif;
?>
<?php
if(isset($_POST['base'])):
$areat = 1/2 * $_POST['base'] * $_POST['height'];
echo "The Area of a Triangle is ";
echo round($areat, 3);
endif;
?>
<?php
if(isset($_POST['length'])):
$perimeter=($_POST['length']+$_POST['width'])*2;
echo "The Perimeter of the Rectangle is ";
echo round ($perimeter,4);
endif;
?>
<?php
if(isset($_POST['radius'])):
$Pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067;
$diameter= ($_POST['radius'] * 2);
$areac= $Pi * $_POST['radius'] * $_POST['radius'];
$circum= 2 * $Pi * $_POST['radius'];
echo "The Area of the Circle is ";
echo $_POST['radius'];
echo round($areac,3);
echo "<br>The Circumference of the Circle is ";
echo round($circum,3);
echo "<br>The Diameter of the Circle is ";
echo round($diameter,3);
endif;
?>
<form action="process.php" method="post">
Input again?: <input type="submit" value="Yes" name="yes">
<input type="submit" value="No" name="no">
</form>
This is my code on PHP process file:
<?php
function formFunction(){
if(isset($_POST['formula'])):
$selection=$_POST['formula'];
switch($selection):
case 'default':
echo "Please select a formula";
break;
case '1':?>
<form action="output.php" method="post">
Radius: <input type="text" name="radius">
<input type="submit" value="Compute">
</form>
<?php
break;
case '2':?>
<form action="output.php" method="post">
Fahrenheit: <input type="text" name="f-value">
<input type="submit" value="Compute">
</form>
<?php
break;
case '3':?>
<form action="output.php" method="post">
Base: <input type="text" name="base">
Height: <input type="text" name="height">
<input type="submit" value="Compute">
</form>
<?php break;
case '4':?>
<form action="output.php" method="post">
Length: <input type="text" name="length">
Width: <input type="text" name="width">
<input type="submit" value="Compute">
</form>
<?php break;
endswitch;
endif;}?>
<?php
//I tried looping here but doesn't seem to work fine. It works but it displays infinite loop or it doesn't display at all
?>

add these blocks of code in process.php
if(isset($_POST['yes'])) :
header("Location: https://file_path.php");
die();
endif;
if(isset($_POST['no'])):
echo "Thank You!";
endif;
here, in header write your file path where you want to redirect like if you are using localhost than header("Location: localhost/folder_name/file_name.php");
if still you have queries comment down i will help you out till i can

Related

I made this little calculator, but it isn't showing the output

It isnt showing output, it refreshes but result isn't showing. i wonder what i'm doing wrong?
I just want the output shown in the same page, below the submit button it should be centered as well, but it aint working, kindly help me out guys, Thanks.
<html>
<head></head
<body>
<form method="post" align="center">
<input type="text" name="FirstValue" placeholder="Enter First Number"><br>
<input type="text" name="Operator" placeholder="Enter Operator / * + -"><br>
<input type="text" name="SecondValue" placeholder="Enter Second Number"><br><br>
<input type="submit" value="Submit"><br><br>
</form>
<?php
if(isset($_POST['submit'])){
$N1 = $_POST['FirstValue'];
$OP = $_POST['Operator'];
$N2 = $_POST['SecondValue'];
switch($OP){
case '/':
echo "Division Result = ".$N1/$N2;
break;
case '*':
echo "Multiplication Result = ".$N1*$N2;
break;
case '+':
echo "Addition Result = ".$N1+$N2;
break;
case '-':
echo "Subtraction Result = ".$N1-$N2;
break;
default:
echo "Invalid Option";
}
return 0;
}
?>
</body>
</html>
You are missing name="submit". Full html form:
<form method="post" align="center">
<input type="text" name="FirstValue" placeholder="Enter First Number"><br>
<input type="text" name="Operator" placeholder="Enter Operator / * + -"><br>
<input type="text" name="SecondValue" placeholder="Enter Second Number"><br><br>
<input type="submit" name="submit" value="Submit"><br><br>
</form>
In a real example you need to sanitize inputs before using or echoing to display.
Or you can check numbers $N1 and $N2 with function is_numeric before calculating or using as feedback to user.

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)

Simple form submit to PHP function failing

I'm trying to submit two form field values to a PHP function, on the same page.
The function works perfect manually filling the two values.
<?PHP // Works as expected
echo "<br />"."<br />"."Write text file value: ".sav_newval_of(e, 45);
?>
On the form I must be missing something, or I have a syntax error. The web page doesn't fail to display. I found the below example here: A 1 Year old Stackoverflow post
<?php
if( isset($_GET['submit']) ) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
<?php if( isset($result) ) echo $result; //print the result of the form ?>
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1"></input>
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2"></input>
<br></br>
<input type="submit" value="send"></input>
</form>
Could it be the placement of my code on the form?
Any suggestions appreciated.
You need to name your submit button, or check for something else on your if(isset($_GET['submit'])) portion:
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1" />
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2" />
<br></br>
<input type="submit" name="submit" value="send" />
</form>
OR keep same form, but change php to:
<?php
if( isset($_GET['val1']) || isset($_GET['val2'])) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
You can you hidden field as:
<input type='hidden' name='action' value='add' >
And check on php by using isset function that form has been submitted.

to add an input element to the form dynamically using only PHP

I would like to add an input element to the form dynamically using only PHP.
I know how to make this using php and JavaScript combination, thus do nto advice abotu JavaScript.
The example below does not work. Could you please advice and comment:
input.php
<br> <input type="text" name="mob[]" value="" size="3" >
form.php
<?php
if( isset($_POST['AddNum']) ){
$AddNumCount=$_POST['AddNumCount'];
$AddNumCount=$AddNumCount+1;
echo $AddNumCount;
}
if( isset($_POST['register']) ){
print_r($_POST['register']);
}
if (!isset($AddNumCount)) {$AddNumCount=5;}
?>
<form action="" method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<br>
<?php for ($i=0; $i<$AddNumCount; $i++) { Include('input.php'); } ?>
<br> Add number: <input type="submit" name="AddNum" form="form1" value="Add NUmber"> </p>
<input type="hidden" name="AddNumCount" form="form1" value=" <?php $AddNumCount; ?> "> </p>
<br></form><input type="submit" name="register" id="regcont" value="register"> </p>
</form>
Maybe you know how to make single submit button for many forms?
I mean each input would be a separare form and all forms can be submittted with the button on the end?
You use two action attrs. Maybe you mean:
<form method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
For submitting many forms by one button - you need to use JavaScript and send them in cycle via AJAX.
I am sorry i change this post. This is working example for dynamical PHP.
Use EXform.php. Other files are generated or helping.
Maybe it is also possible to make this using Session variables and header for redirecting to regenerated webpage.
EXform.php
<?php if (isset( $_POST['AddNum'])) { Include("GENinput.php"); } ?>
<?php if (!isset( $_POST['AddNumCount'])) { $_POST['AddNumCount']=1; Include("GENinput.php"); } ?>
<form action="" method="post" id="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<?php Include("INCinput.php"); ?>
<br> Add number: <input type="submit" name="AddNum" form="form1" value="Add NUmber"> </p>
<input type="hidden" name="AddNumCount" form="form1" value="<?php echo $AddNumCount; ?>"> </p>
<input type="submit" name="register" id="regcont" value="register"> </p>
<br></form>
</form>
GENinput.php // generates included file
<?php
if( isset($_POST['AddNum']) ){
$AddNumCount=$_POST['AddNumCount']; //=
$fnameinp="INCinput.php";
$fileinp=fopen($fnameinp,"w");
$_POST['AddNumCount']=$AddNumCount=$AddNumCount+1;
//echo "AddNumCount=".$AddNumCount;
$strV=""; $stri="";
for ($i=0; $i<$AddNumCount; $i++) {
$strV.=" \n
<?php
if( isset(\$_POST['v']['tname']['colname'][".$i."]) )
{ \$v['tname']['colname'][".$i."]=\$_POST['v']['tname']['colname'][".$i."];}
else { \$v['tname']['colname'][".$i."]=".$i."; }
?>
";
$stri.=" <br> <input type=\"text\" name=\"v[tname][colname][".$i."]\" value=\"<?php echo \$v['tname']['colname'][".$i."]; ?>\" > \n\n";
}
fwrite($fileinp,$strV);
fwrite($fileinp,$stri);
fclose($fileinp);
}

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

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.

Categories