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***
Related
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 have a form that I use to insert data into database and a table in which I display the records from the same database. I have the update button on each record displayed.
When I click on it, the data from the database is displayed in each input from the form; I've modified the information that I want and click Update button from the form.
The problem is that the data I have modified is not updated in the database and is not displayed in the table of my page.
My index-admin.php file:
<?php
include("functions.php");
//fetch record to be updates
if(isset($_GET['update'])){
$id = $_GET['update'];
$update = true;
$query="SELECT * FROM utilizatori WHERE id_user= $id";
$record = mysqli_query($conn,$query);
if (count($record) == 1 ) {
$rec = mysqli_fetch_array($record);
$id=$rec['id_user'];
$nume=$rec['nume'];
$prenume=$rec['prenume'];
$email=$rec['email'];
$pwd=$rec['pass'];
$rol=$rec['rol'];
}
}
?>
My form:
<form method="POST" action="functions.php">
<input type="hidden" name="id" value="<?php echo $id; ?> ">
<div class="input-group">
<label>Nume</label>
<input type="text" name="nume" value="<?php echo $nume; ?>">
</div>
<div class="input-group">
<label>Prenume</label>
<input type="text" name="prenume" value="<?php echo $prenume; ?>" >
</div>
<div class="input-group">
<label>Email</label>
<input type="text" name="email" value="<?php echo $email; ?> ">
</div>
<div class="input-group">
<label>Password</label>
<input type="text" name="pass" value="<?php echo $pwd; ?> ">
</div>
<div class="input-group">
<label>Rol</label>
<input type="text" name="rol" value="<?php echo $rol; ?> " >
</div>
<div class="input-group">
<?php if ($update == false): ?>
<button type="submit" name="save" class="btn">Save</button>
<?php else: ?>
<button type="submit" name="update" class="btn">Update</button>
<?php endif ?>
</div>
</form>
My functions.php file:
if(isset($_POST['update'])) {
$nume = mysqli_real_escape_string($conn,$_POST['nume']);
$prenume = mysqli_real_escape_string($conn,$_POST['prenume']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$pwd = mysqli_real_escape_string($conn, $_POST['pass']);
$rol = mysqli_real_escape_string($conn, $_POST['rol']);
$query="UPDATE utilizatori
SET nume='$nume',
prenume='$prenume',
email='$email',
pass='$pwd',
rol='$rol'
WHERE id_user=$id;";
mysqli_query($conn,$query);
$_SESSION['msg'] = 'Date actualizate!';
header('Location:index-admin.php');
}
It seems that $id is undefined within functions.php. It only existed in index-admin.php, and when the form is submitted, that code is not running. Web applications are stateless and variable values do not persist between requests. Nor are variable values magically passed between separate script files (unless they are called within the same request via an "include" or "require" statement).
However you've actually partially solved that. You have already placed the ID within a hidden field in your form when index-admin is being loaded. Now you just need to create a new variable to read it in functions.php:
if(isset($_POST['update'])) {
$id = $_POST["id"];
As a separate point, I can't see why you also wrote include("functions.php"); within the index-admin script...your form posts back directly to functions.php, rather than to index-admin.php. It makes no sense to include functions.php within that page...it won't do anything useful as far as I can see.
Please pay attention to the warnings within the comments about SQL Injection. This is a serious vulnerability and you should fix it as soon as possible, preferably before you commence testing your code (so that you don't have to re-test it once you've re-written the query code).
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
From the HTML page I am submitting some records to a PHP page, where the records will be saved to the DB and then show a
message saved successfully
message to the user on the same HTML page.
When i submit my form, the records are sent to the HTML page and it displays the success message on another page and not on the same HTML page (where the form code is written). How can i correct it?
HTML
<form action="save.php" class="tsc_form_contact_dark nolabel" method="POST">
<input type="text" name="name" class="form-input" placeholder="Name (required)" required />
<input type="email" name="email" class="form-input" placeholder="Email (required)" required />
<input class="form-btn" type="submit" value="Send Message" />
</form>
PHP
<?php
$name = $_POST["name"];
$email = $_POST["email"];
// Do other stuff
echo "Success";
?>
All you need to do is set a message variable. Something like this:
$msg=isset($_GET['msg']) ? $_GET['msg'] : "";
<div><?php echo $msg; ?></div>
<form action="save.php" class="tsc_form_contact_dark nolabel" method="POST">
//rest of the form
And in the php, you need to redirect to the form page something likt this:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
// Do other stuff
$msg = "Success";
$redirecturl = "form_page.php?msg=".$msg;
header("Location: $redirecturl");
?>
There could be other methods to send result message using session, but I would recommend not doing that.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
$redirect=$_GET["r"];
if ( isset($_GET["r"]) ){
header("location: http://" .$redirect);
} else {
$redirect = "mickiewiki.nl/login/profile.php";
header("location: http://" .$redirect);
this code won't work? I want it to be like if I go to mickiewiki.nl/login/login.php?r=mickiewiki.nl/Doneer.php that when I login I go back to the page Doneer.php
When I login, I am always being sent to profile.php no matter if I add my r or not
<?php
//allow sessions to be passed so we can see if the user is logged in
session_start();
//connect to the database so we can check, edit, or insert data to our users table
$database = mysql_select_db('***', $con) or die(mysql_error());
//include out functions file giving us access to the protect() function made earlier
include "login/functions.php";
$selectedid = $_GET['id'];
if(empty($selectedid)) {
$selectedid = $_SESSION['uid'];
}
if(strcmp($_SESSION['uid'],"") == 0){
header("location: login/login.php?r=mickiewiki.nl/Doneer.php");
} else {
?>
In your form on your website, you are not setting r through GET. You have username, password & submit for your form - and nothing for GET. If you are submitting r with your form (which I cannot see that you are), then you should probably be checking for $_POST['r'] instead of $_GET['r'].
Basically, you need to add a hidden field for your r variable and submit that along with your form.
<form method="post" action="login.php">
<input type="hidden" name="r" value="<?php print $_GET['r']; ?>">
<p><label for="name">Username: </label>
<input type="text" name="username" /></p>
<p><label for="pwd">Password: </label>
<input type="password" name="password" /></p>
<p>
<input type="submit" id="submit" value="Login" name="submit" />
</form>
but, you must do a control for $_GET["r"] ... and why it doesn't work?