PHP overriding isset value, showing new output - php

So, I have this code
<?php
if(!isset($_POST["step1_submit"]))
{
echo "Fill step1!";
step1();
}
function step1()
{
if(isset($_POST["step1_submit"]))
{
step2();
}
else
{
echo '<form action="" method="post">STEP 1: <input name="step1_input"/><input name="step1_submit" type="submit" value=">>STEP 2>>"/></form>';
}
}
function step2()
{
echo "reached step 2";
if(isset($_POST["step2_submit"]))
{
echo '<br/>'.$_POST["step2_input"];
step3();
}
else
{
echo '<form method="post">STEP 2: <input name="step2_input"/><input name="step2_submit" type="submit" value=">>STEP 3>>"/></form>';
}
}
function step3()
{
echo '<strong>WELL DONE</strong>';
}
?>
It shows the input for step1, but never gets to show step2. In my opinion, it gets stuck on calling the function step2, because while doing it, the isset($_POST(step1_submit)) changes it value to NULL.
How can I manage to make this code work? It should be like: filling step 1 input >> getting to fill step 2 input >> submit step 2 >> get to see the 'WELL DONE' echo.

Basically the conditional statement on top needs an else branch:
if(!isset($_POST["step1_submit"]))
{
echo "Fill step1!";
step1();
} else {
step2();
}
...
However, if you have more steps it should look like this:
switch(TRUE) {
case isset('step1_submit') :
step2();
break;
case isset('step2_submit') :
step3();
break;
...
default:
echo "Fill step1!";
step1();
}
Then change your functions to:
function step1()
{
echo '<form action="" method="post">STEP 1: <input name="step1_input"/><input name="step1_submit" type="submit" value=">>STEP 2>>"/></form>';
}
function step2()
{
echo '<form action="" method="post">STEP 2: <input name="step2_input"/><input name="step2_submit" type="submit" value=">>STEP 3>>"/></form>';
}
...
The if conditionals aren't required there.

Related

Trying to echo condition by given input but results are three times I want single result condition by given input

**hi here is the code please answer my question. I'm trying to echo condition by given input but results are three times I want single result condition by given input **
<?php
$arrayName = array('bravo', 'alpha', 'jhony');
foreach ($arrayName as $key) {
if (isset($_REQUEST['num1']) && $_REQUEST['num1'] == $key) {
echo "yes". $_REQUEST['num1']. "Available";
} else {
echo "Sorry!".$_REQUEST['num1']." is not available";
}
}
?>
<form action="" method="get" accept-charset="utf-8">
<input type="text" name="num1">
<button type="submit">submit</button></form>
Rather than loop through your white list, use in_array:
$arrayName = array('bravo','alpha','jhony' );
if(isset($_REQUEST['num1']) && in_array($_REQUEST['num1'], $key)){
echo "yes". $_REQUEST['num1']. "Available";
}
else{
echo "Sorry!".$_REQUEST['num1']." is not available";
}

PHP Captcha refuses to validate

I had this working, then out of the blue something changed and my php captcha fails constantly. I feel like it could be a server side issue, but I can't find anything wrong.
I'm using
https://www.phpcaptcha.org/
https://github.com/dapphp/securimage
I'm testing this at the root level of the site.
<?php
session_start();
require_once('securimage/securimage.php');
?>
<form class="" method="post" action="" enctype="multipart/form-data" id="captcha">
<?php
$options = array();
$options['namespace'] = 'captcha';
$options['input_id'] = 'captcha_code';
echo Securimage::getCaptchaHtml($options, Securimage::HTML_IMG);
echo Securimage::getCaptchaHtml($options, Securimage::HTML_ICON_REFRESH);
echo Securimage::getCaptchaHTML($options, Securimage::HTML_INPUT);
?>
<input type="submit" value="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$captcha_code = isset($_POST['captcha_code']) ? $_POST['captcha_code'] : null;
$image = new Securimage();
var_dump($_SESSION);
if($image->check($captcha_code) == true)
{
echo "Captcha was good!";
}
else
{
echo "Captcha did NOT match!";
}
}
?>
The captcha $SESSSION[]'s get set properly.
array(4)
{
["securimage_code_disp"]=> array(1)
{
["captcha"]=> string(6) "LG2BE2"
}
["securimage_code_value"]=> array(1)
{
["captcha"]=> string(6) "lg2be2"
}
["securimage_code_ctime"]=> array(1)
{
["captcha"]=> int(1457447465)
}
["securimage_code_audio"]=> array(1)
{
["captcha"]=> NULL
}
}
But no matter what i try, the if condition always fails. Returns False.
if($image->check($captcha_code) == true)
{
echo "Captcha was good!";
}
else
{
echo "Captcha did NOT match!";
}
Everything is working except the validation part. It refuses to work.

Switch Statements not working

I'm trying to use the switch statements but it's not working. My problem is for example i input LazyBoy in the textbox, that should echo LazyBoy else echo another string.
<?php
$classmap = $_POST['classmap'];
switch ($classmap) {
case "LazyBoy":
echo "You're Lazy!";
break;
case "GrayHounds":
echo "You're Gray!";
break;
}
?>
Here is the form -
<form action="checkout.php" method="post" >
<input type="hidden" name ="classmap" value="<?php include('db.php');
$origin = $_POST['origin'];
$class = $_POST['class'];
$daten = $_POST['daten'];
$result = mysql_query("SELECT * FROM route WHERE route LIKE '%$origin%' AND type LIKE '%$class%' AND date LIKE '%$daten%' ");
while($row = mysql_fetch_array($result))
{
echo $row['type'];
} ?>">
</form>
You just need to check if the post variable is set and/or add a default case :
if(isset($_POST['classmap'])) {
$classmap = $_POST['classmap'];
switch ($classmap) {
case "LazyBoy":
echo "Your Lazy!";
break;
case "GrayHounds":
echo "Your Gray!";
break;
default:
echo "Something";
break;
}
}
else {
echo "Something";
}
Make sure you post to the right script.
Try to put default statement to debug.
$classmap = $_POST['classmap'];
switch ($classmap) {
case "LazyBoy":
echo "You're Lazy!";
break;
case "GrayHounds":
echo "You're Gray!";
break;
default:
echo "does not match with previous cases";
}
First you have to post values to define the classmap index.
<form method="post">
<input type="text" name="classmap" value="LazyBoy"/>
<input type="submit" value="submit"/>
</form>
<?php
$classmap = (isset($_POST['classmap']) ? $_POST['classmap'] : null);
switch ($classmap) {
case "LazyBoy":
echo "You're Lazy!";
break;
case "GrayHounds":
echo "You're Gray!";
break;
}
?>
Your Code is correct.
I guess there is a Problem with the $_POST.
Try to verify it.
$classmap = "LazyBoy";

In if condition pass post value with condition operrator

I want to post conditional operator from Form Input. And in IF condition use the posted operator. Unable to do so any logic to gt it done.
<form action="" method="post" accept-charset="utf-8">
<input name="postvalue" size="5" maxlength="7" value=">=5">
<p><input type="submit" value="Go"></p>
</form>
<?php
if(10 $_POST["postvalue"]) {
echo "Its greater than 5";
} else {
echo "Its less than 5";
}
?>
You are probably looking for something like this:
<form action="" method="post" accept-charset="utf-8">
<input name="term" size="5" maxlength="7" value=">=5">
<p><input type="submit" value="Go"></p>
</form>
<?php
// separate operator and operand from the posted value
preg_match('/^([=<>!]+)([0-9]+)$/', $_POST['term'], $tokens);
$operator = $tokens[1];
$operand = $tokens[2];
// create an evaluation function
$check = create_function('$value', '
$operand = '.$operand.';
switch("'.$operator.'") {
case "==": return ($value==$operand);
case "!=": return ($value!=$operand);
case "<": return ($value<$operand);
case "<=": return ($value==$operand);
case ">": return ($value>$operand);
case ">=": return ($value>=$operand);
default: throw new Exception("invalid operator");
}');
// apply the evaluation function to some value
try {
if ($check(10)) {
echo "10 is greater than 5";
} else {
echo "10 is less than 5";
}
} catch (Exception $e) {
echo sprintf('Exception: %s', $e->getMessage());
}
?>
Obviously error detection has to be added and the like...
Anyway this is a pretty "strange" architecture... Consider other approaches, since this approach is somewhat prone to failures. Instead you should post operand and operator in separate input fields (post fields) to simplify evaluation and make things more robust. The operator probably should be offered as a select input, the operand as a number spinner...

PHP actions if checkboxes are selected!

I have a form with 3 checkboxes, and my idea is run some commands if they are selected and do something else if they are not selected.
for($i=1; $i<=3; $i++)
{
if ($_POST['option'.$i])
{
echo "123";
}
if (!$_POST['option'.$i])
{
echo "456";
}
}
But if they are not selected the commands are not executed.. The if statement is correct?
No, what you should be doing is checking them like this:
if (isset($_POST['option'.$i]))
Otherwise, you are just trying to evaluate the boolean form of whatever is in that $_POST element. Why is this bad? Suppose the value of that field were 0. Even though the checkbox was checked, your code wouldn't run.
Documentation for isset()
Sure, that will work just fine.
If you want to slightly de-uglify your code, you can do it this way:
<input type="checkbox" name="options[option_name]" value="1" />
<input type="checkbox" name="options[second_option_name]" value="1" />
if(isset($_POST['options']) && is_array($_POST['options']) {
foreach($_POST['options'] as $option_name => $ignore_this) {
switch($option_name) {
case 'option_name':
// do something
break;
case 'second_option_name':
// do something else
break;
}
}
}
You can use an if ... else:
if ($_POST['option'.$i])
{
echo "123";
}
else
{
echo "456";
}
to avoid checking the same condition twice.

Categories