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 can make a simple set of IF statements to find whether a number is either a composite or a prime (even though I think a function would be better suited).
However, I can't echo out either statement below when I try to up-my-game and use post.
There are no syntax errors here, just flawed logic. Problem is, where is the flaw??
Thanks for any advice.
<form>
<form name="PrimeCalculator" method="post" action="">
<input type="text" name="input"> Enter any number to see if it's a Prime or Composite<br />
<input type="submit" name="submit" value="Calculate">
</form>
<?PHP
if($_POST['submit'] == "Calculate"){
$num=$_POST["input"];
for ($i = 2; $i <= $num-1; $i++) {
if ($num % $i == 0) {
$value= True;
}
}
if ($value) {
echo 'The Number '. $num . ' Is A Composite';
} else {
echo 'The Number '. $num . ' Is A Prime';
}
}
?>
Your error actually is in the html, remove the first <form> tag and it should work, though your code is throwing
E_NOTICE : type 8 -- Undefined variable: value -- at line 14
when entering a prime number as $value isn't set then.
You can use
if (isset($value) && $value)
for the check instead.
<form name="PrimeCalculator" method="post" action="">
<input type="text" name="input"> Enter any number to see if it's a Prime or Composite<br />
<input type="submit" name="submit" value="Calculate">
</form>
Related
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 4 years ago.
Improve this question
I have text box:
Size of array:
After this i have button Show array:
<html>
<body>
<form name="form" method="post">
Size of array: <input type="number"value="5" name="size", min="2", max="10">
<input type="submit" name="Send" value="Show array"></span></p>
</form>
</body>
</html>
<?php
if(isset($_POST['Send']))
{
//what must be here?
}
?>
but i don't know, how to generate random numbers from digits, which is entered in text box, e.g. if text box contains 8, then generates 8 random numbers from 2 until 10..can u help please ?
Edit new version i think is what you want??
$numb=range(2,10);
shuffle($numb);
$output = array_slice($numb, 0, $_POST['guess']);
echo implode(',',$output);
range() to make an array of the numbers you want to use (2-10). shuffle randomises them, array_slice() picks the number of elements according to the form input. implode for display.
Old version:
<html>
<body>
<form name="form" method="post">
Size of array: <input name="guess" type="number" value="5" name="size", min="2", max="10">
<input type="submit" name="Send" value="Show array"></span></p>
</form>
</body>
</html>
<?php
if(isset($_POST['Send']))
{
for ($i = 0; $i < $_POST['guess']; $i ++) {
$random[] = rand(2, 10);
}
echo implode(',', $random);
}
?>
the for loop is running the number of times the user selected, and the rand is picking the numbers, implode is gluing the numbers together for output
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I wrote a code such that if I fill all fields of form and I submit then I should get "Ok"
and if I left the form blank then I should get "Please fill all fields".
I am not getting any errors so I am confused how to proceed .
<?php
if(isset($_POST['submit_text']) && isset($_POST['find_word']) && isset($_POST['replaced_word'])){
$sub_text = $_POST['submit_text'];
$fin_text = $_POST['find_word'];
$rep_text = $_POST['replace_word'];
if(!empty($sub_text )&& !empty($fin_text) && !empty($rep_text)){
echo "Ok.";
}else{
echo "Please fill all fields.";
}
}
?>
<html>
<head>
</head>
<body>
<form id="myForm" name="myForm" action="index.php" method="post" >
<p>
Submit Text<br/>
<textarea rows="5" cols="40" type="text" id="submit_text" name="submit_text" ></textarea>
</p>
<p>
Find<br/>
<input id="find_word" name="find_word" type="text" />
</p>
<p>
Replace<br/>
<input id="replace_word" name="replace_word" type="text" />
</p>
<input id="submit" name="submit" type="submit" value="Submit" />
</form>
</body>
</html>
For any query please comment below.
you have missed few characters here
isset($_POST['replaced_word'])
you have to use same name you use in input field.like this
isset($_POST['replace_word'])
Your first "if" needs an "else", I think it would trigger that in your scenario.
I generally try separating these, and do not put them into one large IF..
for example, I would do an if isset and else on each one separately, converting to a different variable accordingly before doing the rest of the logic.
In the first if, you wrote "replaced_word", but the name of the input tag is "replace_word" without the "d". So $_POST['replaced_word'] is never set so that's why it's not working.
I'm looking to implement a very simple quiz using PHP which I'm struggling to find any other examples of to copy. I'm open to various solutions but the requirements are as follows:
Quiz takes place on a single page;
One questions appears at a time, and the user can only see the next question when they correctly answered the current one;
All question types are 'short answer', with users needing to input the correct answer in a text box.
I have been attempting to find a solution using PHP conditional statements (if/else) based on a users answers and my current code looks like this:
<form method="POST" action="">
<label for="question01">1. How many sides does a triangle have?</label>
<input type="text" name="guess01" value="">
<input type="submit">
</form>
<?php
// Process Question 1 Form
$guess01 = $_POST['guess01'];
$guess01 = strtolower($guess01);
$answer01 = "three";
if ($guess01 == $answer01)
{
?>
<p>Correct answer given for question 1!</p>
<form method="POST" action="">
<label for="question02">2. How many sides does a square have?</label>
<input type="text" name="question02" value="" placeholder="Answer here...">
<input type="submit">
</form>
<?php
// Process Question 2 Form
$guess02 = $_POST['guess02'];
$guess02 = strtolower($guess02);
$answer02 = "four";
if ($guess02 == $answer02)
{
?>
<p>Correct answer given for question 2!</p>
<form>
<label for="question02">3. How many sides does a pentagon have?</label>
<input type="text" name="question02" value="" placeholder="Answer here...">
<input type="submit">
</form>
<?php
// Process Question 2 Form
$guess03 = $_POST['guess02'];
$guess03 = strtolower($guess02);
$answer03 = "five";
if ($guess03 == $answer03)
{
?>
<p>Correct answer given for question 3!</p>
<p>Quiz completed!</p>
<?php
}
elseif (empty($guess03))
{
echo "<!-- No Answer Given -->";
}
else
{
echo "<p>Try Again!</p>";
}
?>
<?php
}
elseif (empty($guess02))
{
echo "<!-- No Answer Given -->";
}
else
{
echo "<p>Try Again!</p>";
}
?>
<?php
}
elseif (empty($guess01))
{
echo "<!-- No Answer Given -->";
}
else
{
echo "<p>Try Again!</p>";
}
?>
I'm sure this is very inefficient coding and I'm open to other ways of tackling this. Any help would be very gratefully received. Thank you.
As the form and the validation of your questions are always same only the data changes, you do not need to copy the code. Just put questions and answers in an array and put the number of the current questions in your form.
// Declare all your questions and answers as multi dimensional array
$questionsAndAnwsers = array(array("question" => "1. How many sides does a triangle have?", "answer" => "three"),
array("question" => "2. How many sides does a square have?", "answer" => "four"));
// current question
$currentQuestion = 0;
if(isset($_POST["currentQuestion"])){
$currentQuestion = $_POST["currentQuestion"];
if($_POST["guess"] == $questionsAndAnwsers[$currentQuestion]["answer"]){
// answer was correct, increment your question-counter for the next question
$currentQuestion++;
// print success message
} else {
// question was wrong, counter stays as it is, so same question will be printed
// print failure message
}
}
?>
<form method="POST" action="">
<label for="question"><?php echo ($currentQuestion+1).". ". $questionsAndAnwsers[$currentQuestion]["question"];?></label>
<input type="hidden" name="currentQuestion" value="<?php echo $currentQuestion;?>">
<input type="text" name="guess" value="">
<input type="submit">
</form>
<?php
You have to add a additional if-clause, for the case all questions has been answered.
EDIT:
This is a simple Implementation and it is easy to cheat. As the question number is passed to the browser, the user can alter it (setting it to 9). To close this whole, you have to use session and save the number of the last correct answer in the session on the server.
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 8 years ago.
Improve this question
I am working with this issue since last few hours and also searched the related questions on stack overflow. I have a simple html form
<form name="user_verification" action="action.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit" name="submit" value="submit">
</form>
and here is the php script in action.php file
if(isset($_POST['submit']))
{
echo 'yes';
}else{
echo 'no';
}
It always display "no". I tested my php script using this
if(1==1)
{
echo 'yes';
}else{
echo 'no';
}
In this case, it displays "yes". This means that problem is with isset($_POST['submit']) function but I can't find out the solution. please help in this regard. thanks
For robustness its best to check the method against the request.
This is a simple example of a form processor validating a post request.
if ('POST' === $_SERVER['REQUEST_METHOD']) {
if (!isset($_POST['required_data'])) {
http_send_status(400);
exit;
}
echo 'OK';
}
You will still need to check with isset against the fields you require.
Maybe somewhere $_POST values are emptied/unseted. This may be due to php configuration or as security measure (i.e. http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading). You may check $_REQUEST and also check if you can get $_GET values (method of form is get).
Your code is correct, it's working for me. See this for more info.
In index.php
<form name="user_verification" action="action.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit" name="submit" value="submit">
</form>
in action.php
<?php if(isset($_POST['submit']))
{
echo 'yes';
}else{
echo 'no';
} ?>
The above code will only display the submitted values if the submit button was clicked.
isset( ). This is an inbuilt function that checks if a variable has been set or not. In between the round brackets, you type what you want isset( ) to check. For us, this is $_POST['Submit']. If the user just refreshed the page, then no value will be set for the Submit button. If the user did click the Submit button, then PHP will automatically return a value
$var = '';
// This will evaluate to TRUE so the text will be printed.
if (isset($var)) {
echo "This var is set so I will print.";
}
You may also use the var_dump(isset()); // TRUE
to output the return value of isset().
Your code is correct.
It works only when you submit the form.
So, unless you submit the form, it will always print no.
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
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="post">
Text that needs to be processed
<input type="text" id="text" name="t">
<input type="submit" id ="order" name="order">
<input type="text" name="credit" value="100" readonly="true" id="credit"/>
</form>
<?php
if(isset($_POST['order']))
{
print_r($_POST['t']);
}
?>
</body>
</html>
How do I manipulate the value of "credit" on click of submit (i want to be able to set the value of the credit field to the $_POST['t] value
After seeing your comment, this is easily achievable by doing this:
<input type="text" name="credit" value="<?php echo (isset($_POST['order'])) ? $_POST['t'] : '100';?>" readonly="true"id="credit"/>
Using the shorthand ternary operator (?:) (A shorter if/else)
You're basically saying
echo (IF POST[order] IS SET) ? SHOW POST[t] : OTHERWISE SHOW 100;
Pseudo code above
Edit: in relation to your next comment
if(isset($_POST['order'])) {
$t = $_POST['t'];
$v = $_POST['credit'];
$total = ($t - $v);
}
and further on (in the form)
<input type="text" name="credit" value="<?php echo (isset($total)) ? $total : '100';?>" readonly="true"id="credit"/>
Untested Pseudo code above. You'll want to make sure that the numbers don't decrease below 0 possibly?
Another edit based on your comment here
You're on the right path, you just have to set up the conditions:
if(isset($_POST['order'])) {
$t = $_POST['bet'];
$v = $_POST['credit'];
if($_POST['bet'] > $_POST['credit']) {
$total = $v;
echo "<h1>YOUR BET IS TOO HIGH MAN</h1>";
} else {
$total = ($t - $v);
}
}