HTML and PHP webdesign and formating [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I nedd some help formating HTML for my website. I have created a html page for my contact form. Then a php page that corrects errors in my page. then I also created another php page that thanks the person for submitting a contact form. After they submit the contact form I want them php to do the following:
On the form processing page:
say thank you
Echo back the users first and last name.
Show today's date
Echo back the visitor's age in years
If the visitor is older than 55 indicate that they are entitled
to a senior citizen discount
If the visitor is 30-54 years old indicate that they are
entitled to a middle aged citizen discount
If the visitor is younger than 17 indicate that they are entitle to a
youth discount
<?php echo "thanks for contacting us";?>

See..for you above provided code you can do this in PHP and HTML by..
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
//assuming you used this for age and have the rest form
<input type="number" min="0" max="100" name="age">
</form>
<?php
$age = $_POST['age'];
echo date("m.d.y");
echo "Your age is :".$age. ", you are entitled to";
if($age > 55){
echo 'Discount';
}
else if($age > 30 && $age < 55){
echo 'Discount';
}
else if($age < 17){
echo 'discount';
}
?>
It is a simple if-else conditon.
$_POST gets you the value from the form using name tag..I fyou want to generate any HTML tag in PHP just put it inside echo.Like this
echo "<p class="text" id="new1">Sample para tag<p>";
In the similar way you can generate nay html tag in php by just enclosing in echo ' ';

Related

How to print all even numbers between two input values typed by user. please write code hint only in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
How to print all even numbers between two input values typed by user. please write code hint only in php.
I can give you a hint. Lookup the modulo operator. With it you can do something like this:
<?php
if (($number % 2) === 1)
{
echo "$number is odd.";
}
if (($number % 2) === 0)
{
echo "$number is even." ;
}
?>
Also. Have a look at how you ask questions on stack overflow. Or they will prevent you from asking questions in the future if you don't adhere to those rules.
use this code, for user input i have created a form.
<html>
<body>
<form method="post">
<input type="number" name="num1">
<input type="number" name="num2">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
// while loop that will print number
while($num1 <= $num2){
// here is the condition to check the EVEN number
if($num1%2!==0){
echo $num1."<br>";
}
// increasing the loope counter by 1
$num1++;
}
}
?>
i hope now your problem has been solved..

PHP variable in hidden field carries over as a string literal to the next page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm working on a small assignment and ran into an issue I can't seem to figure out. The assignment consists of three pages. The first page consists of a simple field that accepts user input. The second page validates that input, and if valid, displays a second input field. The third page validates page 2 input, then adds it to page 1 input.
I've used a hidden field to store page 1 input on page 2, in order to submit it to page 3. But, because page 2 has to display the form only if the previous input is valid, I implemented the form inside a php conditional statement. So, I directly referenced the php variable holding page 1 input as the hidden field value. Here is my page 2 code:
<?php
if (isset($_POST["get_number1"] )) {
$number1 = $_POST['get_number1'];
$button_pressed = $_POST['sbmt'];
$message;
$error1 = false;
if ($number1==null) {
$message = 'ERROR: input field empty';
$error1=true;
}
else if (!is_numeric($number1)) {
$message = 'ERROR: input must be numeric';
$error1=true;
}
else if (strpos((String)$number1,'.')!=null && strlen((String)$number1)-strpos((String)$number1,'.')>4) {
$message = 'ERROR: input must contain 0 to 3 decimals';
$error1=true;
}
else if (!strcmp($button_pressed,"sbmt")){
$message = 'ERROR: submit button not pressed';
$error1 = true;
}
}
?>
<html>
<head>
<title>GetNumber2.html</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php
if ($error1==true){
echo $message .'<br>'. 'Return to Form 1';
}
else {
echo 'Your number 1: '.$number1.
' <form action="AddNumbers.php" method="post">
<label>Enter the second number (format : 999999.999) :</label>
<input type="text" name="get_number2">
<input type="submit" name="sbmt" value="SubmitNumber">
<input type="hidden" name="hf_number1" value="$number1";>
</form>
';
}
?>
</body>
</html>
The next page displays the hidden field value as $number1, even if I remove the quotation marks. Any hints would be appreciated. Thank you.
Strings with ' (single quotes) are not parsed.
This means that any variable in ' will be considered a string.
Use concatenation, for example:
$str = '<input type="hidden" name="hf_number1" value="' . $number1 . '">';

Display the names of selected checkboxes and the values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to display the name of all the selected check boxes and their values. At the moment it shows the values of all the selected check boxes but not the name of the selected check box. How would I do this?
The sections of code are below. The HTML form section has 9 identical checkboxes apart from the name and value.
<?php
echo $_POST['jan'];
echo $_POST['feb'];
echo $_POST['mar'];
echo $_POST['apr'];
echo $_POST['may'];
echo $_POST['jun'];
echo $_POST['jul'];
echo $_POST['aug'];
echo $_POST['sep'];
?>
<div class="checkbox">
<label>$15 :
<input name="jan" value="15" type="checkbox"> January
</label>
</div>
Any help would be great.
Checkbox parameters are only set if the box is checked. So test whether it's set:
if (isset($_POST['jan'])) {
echo 'jan: ' . $_POST['jan'];
}
You can put this into a loop:
$months = array('jan', 'feb', ...);
foreach ($months as $m) {
if (isset($_POST[$m])) {
echo $m . ': ' . $_POST[$m];
}
}

Creating HTML Forms from PHP Arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to make a form in html that would create radio options bases upon the specified array in PHP.
Code:
<form name="form" action="Test.php" method="get">
<?php
//Creates the Array
$radioButtonArray = array("cat", "dog", "sheep", "moose");
//Length of the Array
$count = count($radioButtonArray);
//Runs for each index.
for($x = 0; $x < $count; $x++)
//Creates a radio button with the specified length
echo "<input=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]} <br>";
?>
</form>
As you can see firstly I open the form tag inside of HTML. Then I create a array with animals names and run a loop through each array index. During each loop it should create a new radio button and then make a new line as directed by the echo.
The issue is that when I run the file the output should be for example:
(RADIO BUTTON HERE) cat
(RADIO BUTTON HERE) dog
(RADIO BUTTON HERE) sheep
(RADIO BUTTON HERE) moose
Instead I get:
cat
dog
sheep
moose
I know that it is reading the echo line so the error would have to be located on that line. I am very new to PHP and decently familiar with HTML so a simple but detailed explanation of what I did wrong or what I should do would be very greatly appreciated. Thank you in advance.
How to Fix:
I did not correctly enter the format for declaring a input.
//Change This
echo "<input=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]}<br>";
//To This
echo "<input type=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]}<br>";
There is small error on the echo statement. HTML radio button should read
but your output statement reads instead.
Hence you should change
echo "<input=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]} <br>";
To
echo "<input type=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]} <br>";
Try this:
echo "<input type=\"radio\" name=\"Animal\" value=\"{$radioButtonArray[$x]}\">{$radioButtonArray[$x]} <br>";
You are not specifying the input type.
input=\"radio\" should be input type=\"radio\"
It should be input type="radio" not input="radio"

Trying to echo a variable ( $i) into another variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special form I have been making that uses some cusotm post types in wordpress. At one point I need to echo a variable $i into an if statement.
There is some validation stuff at the top that will look like this and the code in the loop is below. Pretty much I have been trying to get the majorCause1Error to be majorCause $i Error if you know what I mean, so all up it will be like 1-13
Edit: Sorry If it is hard to see what I am asking, I am finding it really hard to word my problem.
So there is a loop running around the li tags and it echos $i into the name etc so it becomes majorCause1 then next one majorCause2 and the next one magjorCause3 etc etc
Under the labels there is an if statement that is like - if($majorCause1Error !='') { do something } - I want this to be like if($majorCause1Error !=''){} and then the next one be like if($majorCause2Error !=''){} and then if($majorCause3Error !=''){}
Does this make more sense?
Here is a link to the site http://www.foresightaus.com.au/form/
if(trim($_POST['majorCause1']) === '') {
$majorCause1Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause1 = trim($_POST['majorCause1']);
}
if(trim($_POST['majorCause2']) === '') {
$majorCause2Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause2 = trim($_POST['majorCause2']);
}
<li class="fill-in">
<label for="majorCause<?php echo($i); ?>"><?php echo($j); ?>. State one major cause:</label>
<input type="text" name="majorCause<?php echo($i); ?>" id="majorCause<?php echo($i); ?>" value=""/>
<?php if($majorCause1Error != '') { ?>
<span class="error"><?=$majorCause1Error;?></span>
<?php } ?>
</li>
You probably want to be using an array but what you are referencing is called a variable variable and is supported by PHP!
Something like this should do it
${"majorCause{$i}Error"}

Categories