So I want to create a simple result page that lets users download their results using the given code.
This is the script:
<form action="" method="post" >
<input type="text" name="logincode">
<input type="submit" name="send">
</form>
<?php
$name = $_POST['logincode'];
$filename = $name.'/'.$name.'pdf';
header('Location: ./'$filename'');
?>
The principle is when the user writes into the input field, for example (1234) and hits enter, it should redirect him to:
./1234/1234.pdf
I don't know where the mistake is in my code.
Few issues,
Your header should be before anything else as #showdev mentioned in a comment.
You're missing a . between filename and extension
You also have a syntax error in the header trailing ''
And you should exit redirect headers.
You should also be checking your variables as you go, plus check the file exists, so you can show errors.
<?php
// check post request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$errors = [];
// check logincode is set and a number
if (!isset($_POST['logincode']) || !is_numeric($_POST['logincode'])) {
$errors['logincode'] = 'Invalid login code';
} else {
$name = $_POST['logincode'];
// check file is found
if (!file_exists($name.'/'.$name.'.pdf')) {
$errors['logincode'] = 'Your results are not ready.';
}
// now check for empty errors
if (empty($errors)) {
exit(header('Location: ./'.$name.'/'.$name.'.pdf'));
}
}
}
?>
<form action="" method="post">
<?= (!empty($errors['logincode']) ? $errors['logincode'] : '') ?>
<input type="text" name="logincode">
<input type="submit" name="send">
</form>
You are missing a “.” before pdf aren’t you?
And also wrong header('Location: ./'$filename'');
Try this :)
<?php
$name = $_POST['logincode'];
$filename = $name.'/'.$name.'.pdf';
header('Location: ./'.$filename);
?>
It's very insecure code!
Major changes below:
write test for user input data TODO by you
change order PHP block code first, form (HTML) code next in snippet
add test is post request_method before any $_POST['...']; in snippet
add .(dot) before filename extension i $filename in snippet
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['logincode'];
$filename = $name.'/'.$name.'.pdf';
header('Location: ./'$filename'');
}
?>
<form action="" method="post" >
<input type="text" name="logincode">
<input type="submit" name="send">
</form>
<form action="" method="post" >
<input type="text" name="logincode">
<input type="submit" name="send">
</form>
<?php
if($_POST){
$name = $_POST['logincode'];
$filename = $name.'/'.$name.'.pdf';
header('Location: ./'.$filename.'');
}
?>
Related
I have a PHP to verify the HTML form (post method). If the value doesn't match, the error message should be added at the HTML page. I test the embedded JS script in console, it works. But it doesn't work in PHP. How can I fix it? Thank you
Here is my code
<!DOCTYPE html>
<body>
<form action="http://localhost/submitTest.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<div id="submitField">
<input type="submit">
</div>
</form>
</body>
</html>
<?php
if ($_GET["name"] == 1 && $_GET["email"] == 2) {
header('Location: http://localhost/table.html');
} else {
$url = 'http://localhost/submitTest.html';
echo "<meta http-equiv='Refresh' content='0;URL=$url'>";
echo "
<script>
var messageP = document.createElement('span');
messageP.style.color = 'red';
var parent = document.getElementById('submitField');
parent.appendChild(messageP);
messageP.innerHTML = 'not match';
</script>";
}
?>
I want to do something like thiserror message
I have a code which its simplified version could look like this :
file1.php
$array = array();
$array = new randomObject(1);
$array = new randomObject(2);
require('file2.php');
file2.php
<form method="post" action="?">
<?php
foreach ($array as $a) {
<p><?php echo $a->getAValue();
<textarea rows="5" cols="70" name="textbox[]">
</textarea>
</p>
<?php } ?>
<input id="isTrue"> //true or false
<input type="submit" >
</form>
The user is supposed to write answers in the textarea and click on submit then his answers are compared to the randomObject values. Then it shows if it's true or false next to each textarea
You are looking for something that the fron-tend will handle for you and an AJAX call is exactly what you need.
First of all, name your form
<form id="myForm" method="post" action="?">
<?php
foreach ($array as $a) {
<p><?php echo $a->getAValue();
<textarea rows="5" cols="70" name="textbox[]">
</textarea>
</p>
<?php } ?>
<input id="isTrue"> //true or false
<input id="submitButton" type="submit" >
</form>
Now you have proper id's both on the submit button and on the form itself.
<script>
let submitB = document.querySelector("#submitButton");
submit.addEventListener("click", function(e) {
e.preventDefault();
});
</script>
From now on you just have to write a proper ajax call to the url you wanted to access and you will be set to go.
If you need help with that let me know and I will throw something your way.
I am guessing you want to retain the values entered by the user, since they go away if you submit the form. (Page reloads)
This can be done by altering the input fields. If a value was submited pass that value to each corresponding input field.
Something like that:
<textarea rows="5" cols="70" name="textbox[]" <?php if(isset(value[SOMETHING])){?> value="<?php echo value[SOMETHING]; ?>" <?php } ?> >
This is just an example of how it would work. Make sure you adapt it to your code!
I need a form with one input (eg. "number") that after sumbit redirects the broswer to http://staticurl/[number].html
How can I do?
header('location: http://staticurl/'.$_POST['number'].'html');
Maybe?
Assume a HTML form like
<form action="" method="POST">
<input type="number" name="number">
<input type="submit" name="submit" value="submit">
</form>
Now using php you can redirect
<?php
if(isset($_POST['submit'])){
header("Location: http://staticurl/".$_POST['number'].".html");
}
?>
If header() returns any error then you can do it with javascript inside php like
<?php
if(isset($_POST['submit'])){
echo '
<script>
window.location.href = "http://staticurl/'.$_POST['number'].'.html";
</script>
';
}
?>
Validate the form data before processing it.
Solved with
<script type="text/javascript">
function goTo()
{
var NUM= document.forms[0].NUM.value;
var URL = 'http://staticurl/'
window.location = URL+NUM+'.jpg';
return false;
}
</script>
And
<form action="" method="get" onsubmit="return goTo()">
I'm developing a web application and I want to get the answer(yes or no) from user when the deactivate button is pressed. I want to ask him if you want or not to deactivate the account. I'm using PHP to set active status to = 1 when deactivated and 0 to activated.
I want to get the result and verify in PHP if the query runs or not.
I would appreciate if someone helps me, thanks. Here is the code:
if(isset($_POST['desativar']))
{
$login = $_SESSION['login'];
mysqli_query($conexao,"UPDATE usuarios SET ativo_usuario = 1 WHERE login_usuario='$login'");
}
You can use onsubmit attribute.
Example
function confirmDesactiv()
{
return confirm("Are you sure ?")
}
<form method="POST" action="yourphp.php" onsubmit="return confirmDesactiv()">
<button type="submit">Delete my account</button>
</form>
try it.
function confirm_delete(){
if(confirm("Are you sure you want to delete this..?") === true){
return true;
}else{
return false;
}
}
<input type="button" Onclick="confirm_delete()">
You can use PHP to make something like a confirmation box. Below is a little smaple code to show you how:
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['postdata'] = $_POST;
header("Location: ".$_SERVER['PHP_SELF']);
exit;
} else if (isset($_POST['confirm']) && $_POST['confirm'] == 'yes') {
// Do stuff that should only be done after confirming
} elseif (isset($_POST['confirm']) && $_POST['confirm'] == 'no') {
// Cancelled, redirect back to page
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
?>
<?php if (isset($_SESSION['postdata']) && !isset($_POST['confirm'])): ?>
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
Are you sure you want to do this?<br />
<input type="submit" name="confirm" value="yes">
<input type="submit" name="confirm" value="no">
</form>
<?php else: ?>
<!-- Your form here -->
<?php endif; ?>
If you don't want to use AJAX, and assuming your page is called index.php:
<?
$deactivateStatusDisplay = "display: none"; // Initially, do not display a status.
$deactivateStatusMsg = ""; //Status message is initialized as blank.
$queryRan = false; // Just a flag to know if the user clicked deactivation link.
if( 1 === $_GET['deactivate'] ){
$deactivateStatusDisplay = "display: block";
$deactivateStatusMsg = "Your account will be deactivated.";
$queryRan = true;
}
?>
<!DOCTYPE html >
<html>
<head>
<!-- ALL YOUR METADATA -->
</head>
<body>
<div>Do you want to deactivate your account?</div>
DEACTIVATE
<div id="status" style="<? echo $deactivateStatusDisplay ?>"><? echo $deactivateStatusMsg ?></div>
</body>
</html>
**JS**
<script type="text/javascript">
function confirmDesactiv()
{
var flag = confirm("Are you sure ?");
if(flag)
window.open("yourphp.php?id=" + 1);
else
window.open("yourphp.php?id=" + 0);
}
</script>
**HTML**
<form method="POST" action="">
<button type="submit" onclick="confirmDesactiv()">Desactiv my account</button>
</form>
**PHP**
<?php
echo $_GET['id'];
if(isset($_GET['id']) && $_GET['id'])
{
//your update query will come here
}
?>
I have a php page. In the bottom there is a contact form. What I want to do is to hide the contact form when the mail is sent and instead show a thank you part. Means that when customer comes first time to the page the form show up. After submit the thank you part show up.
I have seen it done but have no clue how.
I have an idea that when the page loads it must check a variable to see if the mail was sent, but perhaps this is wrong.
When I need stuff like this, I usually name the submit button "submit" and checks if it is set...
<input type="submit" name="submit" value="Send form">
And the php (Use $_GET or $_POST accordingly)...
<?
if(isset($_POST['submit']))
{
// Show "Thank you"
}
else
{
// Show form
}
?>
When I do things like this I tend to add an button to my form and give it a name
<button type="submit" id="submit" name="submit">Submit</button>
Then in my PHP check if the form has been submitted
<?php
if(isset($_POST['submit'])) {
// Actions After Submit
}
else
{
// Load The Form
}
?>
It's rather quite simple; add exit("Message..."); after mail()
In your PHP where the mail(...) is located, you would do the following:
mail($to,$subject,$message,$headers);
exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");
Here is a complete basic solution using pure PHP, since no code has been provided.
The following is meant to be run as everything inside the same file.
<?php
if(isset($_POST['send'])) {
// Prepare the email
$to = 'email#example.com';
$mail_from = $_POST['email'];
$subject = 'Message sent from website';
$message = $_POST['message'];
$name = $_POST['name'];
$header = "From: $name <$mail_from>";
// Send it
$sent = mail($to, $subject, $message, $header);
if($sent) {
exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");
} else {
echo 'Sorry, your message could not be sent. The error has been logged.';
}
} // closing brace for if(isset($_POST['send']))
?>
<form method="post" action="">
<p>
<label for="name">Name</label>
<input type="text" id="name" name="name" />
</p>
<p>
<label for="email">Email</label>
<input type="text" id="email" name="email" />
</p>
<p>
<label for="message">Message</label>
<textarea id="message" name="message" rows="6" cols="30"></textarea>
</p>
<p>
<input type="submit" name="send" value="Send message" />
</p>
</form>
It's very simple, just if and else,sample form is below
<?php
if(isset($_POST['submit'])) { ?>
//write your other actions on form data
<h2>THANK YOU</h2>
<?php
} else { ?>
<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label><h4>Username:</label>
<input type="text" name="username">
<input type="submit" name="submit" value="submit"/>
</form>
<?php }
?>