php if else not working in this scenario [closed] - php

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.

Related

PHP Form $_POST['button'] always false [closed]

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 months ago.
Improve this question
I'm trying to get submit form event to update a field. I found a test code on internet and that's worked. But when I change the button name for my button nothing happens, and the if always returns false to my button and I have no idea what i'm doing wrong.
Example code works:
<form action="" method="POST">
<input type="submit" name="add" Value="Call Add fun">
<input type="submit" name="sub" Value="Call Sub funn">
<?php echo #$a; ?>
<?php
if(#$_POST['add'])
{
function add()
{
$a="You clicked on add fun";
echo $a;
}
add();
} else if (#$_POST['sub']) {
function sub()
{
$a="You clicked on sub funn";
echo $a;
}
sub();
}
?>
I've tried some variations with if (isset($_POST["course_submit_btn"])) but again nothin happens.
I don't know if it has any interface, but the page url is "panel/create-course/?course_ID=3493/"
My page https://www.file.io/c0pn/download/C1kmP2TblfcU
<form action="" method="POST">
<input type="submit" name="add" Value="Call Add fun">
<input type="submit" name="sub" Value="Call Sub fun">
<?php
if(isset($_POST['add'])) {
add();
} else if (isset($_POST['sub'])) {
sub();
}
function add() {
echo "You clicked on add fun";
// todo: more meaningful functionality
}
function sub() {
echo "You clicked on sub fun";
// todo: more meaningful functionality
}
?>
Your code should work, however there are some tips, you should stick to:
Use consistent code styling (spaces, brackets etc),
Use more meaningful names, in a few days you won't know what variable $a is for
Don't put functions within if sections

HTML Form with PHP: $_POST is not setting on submit [closed]

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 2 years ago.
Improve this question
Background:
I have looked through the variations on this question and solutions, but I have not found one that applies to my situation. I know I'm most likely overlooking something incredibly obvious, but it's been hours and I can't for the life of me pinpoint what it is. I'm following along with this tutorial but making it my own: https://www.taniarascia.com/create-a-simple-database-app-connecting-to-mysql-with-php/.
What happens: When I click the submit button, nothing happens. I see "not set" displayed on the screen when the page loads and after clicking submit, so I know I'm in my else block, which means $_POST is not being set.
What I expect to happen: I expect to see "not set" on page load, but then once I have filled out the form and clicked submit, I expect to see "set" in my echo statement to indicate that I'm in my if statement and isset($_POST['submit']) is true/successful, followed by "Success" indicating that the try code block was successful, plus the values I entered in the form fields appearing in the database. For now, though, I'd just be happy if I got that first "set" to display.
I have tried: I've tried breaking out the php into a separate file and linking it up to the form via action="thatfile.php", but I get the same result. I've also tried using "get" instead of "post" for the method and $_REQUEST instead of $_POST, but again, same outcome - "not set" displayed, no data in the db.
Here is my code:
<?php
if (isset($_POST['submit'])) {
require "../config.php";
echo "set";
try {
$connection = new PDO($dsn, $username, $password, $options);
$new_item = array(
"luggage" => $_POST['luggage'],
"category" => $_POST['category'],
"item" => $_POST['item'],
"description" => $_POST['description']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"stuff",
implode(", ", array_keys($new_item)),
":" . implode(", :", array_keys($new_item))
);
$statement = $connection->prepare($sql);
$statement->execute($new_item);
echo "Success";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else {
echo "not set";
}
?>
<?php include "header.php"; ?>
<h1>Add An Item</h1>
<form method="post">
<label for="item">Luggage</label>
<input type="text" name="luggage" id="luggage"></form>
<label for="item">Category</label>
<input type="text" name="category" id="category"></form>
<label for="item">Item</label>
<input type="text" name="item" id="item"></form>
<label for="description">Description</label>
<input type="text" name="description" id="description">
<input type="submit" name="submit" value="submit">
</form>
Back to home
<?php include "footer.php"; ?>
You are closing the form </form> multiple times before the final end of the form </form> so the submit is not in the form. Remove these:
<label for="item">Luggage</label>
<input type="text" name="luggage" id="luggage"></form> ***HERE***
<label for="item">Category</label>
<input type="text" name="category" id="category"></form> ***HERE***
<label for="item">Item</label>
<input type="text" name="item" id="item"></form> ***HERE***

PHP calculation with values from html form won't work [closed]

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 3 years ago.
Improve this question
I try to calculate the body mass index from values entered in a HTML form, but I don't get any result.
I'm new to php and I tried it for 30 minutes, without any result. Could you help me, please?
<?php
$size = $_POST['size'];
$weight = $_POST['weight'];
?>
<form method="post">
<p>Größe in Zentimeter:
<input type="number" name="size">
</p>
<p>Gewicht in Kilogramm:
<input type="numer" name="weight">
</p>
<p>
<input type="submit" value="Berechnen"
</p>
</form>
<?php
$bmi = weight / (size * size);
echo $bmi;
?>
You are missing the dollar in the beginning of some variables. Also, is important to only do the calculation if the user posts the form:
<form method="post">
<p>Größe in Zentimeter:
<input type="number" name="size">
</p>
<p>Gewicht in Kilogramm:
<input type="number" name="weight">
</p>
<p>
<input type="submit" value="Berechnen"
</p>
</form>
<?php
if (isset($_POST['size'])){
$size = $_POST['size'];
$weight = $_POST['weight'];
$bmi = $weight / ($size * $size);
echo $bmi;
}
?>

Pass textarea value to server php [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 trying to capture the value of a text area and pick you up php so that it kept on the server. The functionality that is what I do is to change texts and photos so pretend that these changes I have made are saved.
Where I can move friends.
Thank you very much for your time .
That's my last try:
if(isset($_GET['submit'])){
$salida = $_GET['textarea'];
$archivo = fopen("prueba2.html", "w+");
fputs($archivo, $salida);
$contenido = file_get_contents($archivo);
fclose($abrir);
}
You are using $_GET variable, but your <form> has method POST. You have to use $_POST variable instead.
Thats it the html form really thank you
<form id="formulario" action="" method="POST" enctype="multipart/form-data">
<div>
<h3>Haga "click" encima del contenido a modificar</h3>
</div>
<iframe id="probando" src="prueba2.html" scrolling="auto" height="700" width="800" marginheight="0" marginwidth="0" name="probando"></iframe>
<textarea name="textarea" rows="4" cols="50"></textarea>
<input id="botonGuardar" type="submit" value="Confirmar cambios"/>
</form>
Give your textarea a name:
<form>
<textarea name="myTextarea"></textarea>
<!-- the rest of your form -->
</form>
And then in your php:
$salida = $_GET['myTextarea'];

How do I Get This HTML 5 Form To Give PHP The Input Data Then Display It On The Screen [closed]

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 8 years ago.
Improve this question
How do I Get This HTML 5 Form To Give PHP The Input Data Then Display It On The Screen. I want to collect data in a form. Convert it into a php variable and then echo it out in a way that I can read it.
<form>
First name:<br>
<input type="text" name="name">
</form>
<body>
<pre>
<?php
$taco = htmlspecialchars($_POST["name"]) . '!';
$taco = $_POST;
echo $taco;
?>
</pre>
</body
</html>
How about the following:
<?php
if($_POST) { // check if the page is posted not with a $_GET
foreach($_POST as $field => $value) { // Foreach field
$value = strip_tags($value); // Strip html tags (you can add more)
echo 'Field: '.$field.' has value: '.$value.'<br>'; // output
}
}
?>
<form action="" method="post">
First name: <input type="text" name="first_name">
<input type="submit" value="Display">
</form>
Just put it all in one php file. You can add as many fields as you want.

Categories