Wordpress Template causes 404 Error - php

I've created a page in Wordpress and used this form as template. But when I submit the form it shows Error 404 page not found.
I've tried various scripts in php but I didnt get fruitful results. Hoping for the best answers. Thanks :)
Here is my coding
<?php
/*
template name: visit
*/
$servername= "localhost";
$username="root";
$password="";
$database="wp_chandan";
if(isset($_POST['name']) && isset($_POST['email']))
{
$name=$_POST['name'];
$email=$_POST['email'];
var_dump($email);
#mysql_connect($servername,$username,$password);
#mysql_select_db($database);
$query=mysql_query("insert into visitor_d (NAME,Email) values ('$name','$email') ");
} ?>
<html>
<head>
<?php get_header(); ?>
<style type='text/css'>
/* form elements */
form {
margin:100px;
padding: 10px 2px;
background: #F5F5F5;
width: 50%;
height:auto;
border:1px solid green;
}
input {
padding:2px;
border:1px solid #eee;
font: normal 1em Verdana, sans-serif;
color:#777;
align:center;
}
input.button {
font: bold 12px Arial, Sans-serif;
height: 24px;
margin: 0;
align:center;
padding: 2px 3px;
color: #333;
background: #e7e6e6 url(MarketPlace-images/button.jpg) repeat-x;
border: 1px solid #dadada;
}
</style>
</head>
<form action="" method="POST">
<fieldset>
<legend><b><u>Visitor Details:</u></b></legend>
Name: <input type="text" name="name" placeholder="Fill Name" required><br><br>
Email: <input type="text" name="email" required placeholder="Enter Email"><br><br>
<input type="submit" name="submit"><br>
</fieldset>
</form>
<?php get_footer();?>
</html>

Make sure that you don't have any reserved WordPress terms in the name attributes in the fields of the form.
WordPress uses many of these reserved terms for it's internal rewriting and as a result if you using name attributes such as "name", it breaks the WordPress rewrite rules and causes this 404 Not Found page after submitting the form.
See more here: http://www.lost-in-code.com/platforms/wordpress/wordpress-404-not-found-when-submitting-form/

html5 doesn't like the empty action attribute, it does cause issues in some browsers. you have not selected a standard so i assume the browser will default to html5
<form action="#" method="POST">
Setting the action as # should work but one thing to watch out for is actions added before your page template is loaded, this can obviously grab the $_POST and redirects would be common so that may also be a reason.
also
Name: <input type="text" name="name" placeholder="Fill Name" required>
WP does not like when you call a field "name" it conflicts with something (i have forgotten the reason by now)
Name: <input type="text" name="aname" placeholder="Fill Name" required>

Related

My web routes request are not coming to controller

I am very new to the laravel web side, i have one page (/) after hitting this route in web it's displaying sign-in page when i fill data and submit register button the request is not coming to my controller, i could not able to figure out why it's happening like this can anyone please suggest me for approach..?
web.php
Route::post('register',[UserController::class,'register']);
Route::get('/', function () {
return view('sign-in');
});
sign-in.blade.php
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
background-color: black;
}
* {
box-sizing: border-box;
}
/* Add padding to containers */
.container {
padding: 16px;
background-color: white;
}
/* Full-width input fields */
input[type=text], input[type=password] {
width: 100%;
padding: 15px;
margin: 5px 0 22px 0;
display: inline-block;
border: none;
background: #f1f1f1;
}
input[type=text]:focus, input[type=password]:focus {
background-color: #ddd;
outline: none;
}
/* Overwrite default styles of hr */
hr {
border: 1px solid #f1f1f1;
margin-bottom: 25px;
}
/* Set a style for the submit button */
.registerbtn {
background-color: #04AA6D;
color: white;
padding: 16px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
opacity: 0.9;
}
.registerbtn:hover {
opacity: 1;
}
/* Add a blue text color to links */
a {
color: dodgerblue;
}
/* Set a grey background color and center the text of the "sign in" section */
.signin {
background-color: #f1f1f1;
text-align: center;
}
</style>
</head>
<body>
<form action="/register" method="POST">
<div class="container">
<h1>Register</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<label for="name"><b>Name</b></label>
<input type="text" placeholder="Enter name" name="name" id="name" required>
<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" id="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" id="psw" required>
<hr>
<p>By creating an account you agree to our Terms & Privacy.</p>
<button type="submit" class="registerbtn">Register</button>
</div>
<div class="container signin">
<p>Already have an account? Sign in.</p>
</div>
</form>
</body>
</html>
UserController.php
protected $request;
public function __construct(UserRequest $request)
{
$this->request = $request;
}
public function register(UserRepository $repository){
$data = $this->request;
$repository->create([$this->request]);
}
You have to include your UserController controller on your web.php file.
<?php
use App\Http\Controllers\UserController.php;
?>
My first guess without diving in too deep is that you are missing the
#csrf
in your POST form. Laravel requires a #csrf token bit included whenever including a non-laravel form.
You can check you are reaching the endpoint by including a
dd($this->request->all()); // or just dd($this->request);
to the top of your register endpoint.
If the request still isn't making it to the backend, make sure your custom request (UserRequest) rules are fulfilled (you didn't include this code so I don't know if all of the input you are submitting are valid in relation to the rules of the request or not)
Side note: It's good practice to name your routes:
Route::post('register',[UserController::class,'register'])->name('your_route_name_here');
And then reference the route name in your form this way:
<form action="{{ route('your_route_name_here') }}" method="POST">
#csrf
<!-- Your form here--->
</form>
Install laravel/ui and take inspiration from the code provided by Laravel

Submission is successful text not going even after reloading

I'm currently working on a small PHP project, which includes a simple form using xampp server. The form is working but the issue that I'm facing is that I want a text "form submission successful" to appear when a user submits the form.
Now the problem is that once someone visits my page for the first time it is not visible but once he submits it then even after reloading the page for several times, it is not disappearing.
Below is my PHP code:
I have declared a variable 'insert', I've first declared it to be false, then I'm changing its value to be true when data is inserted successfully. I'm saying in my HTML file that if the 'insert' value is true then print this.
<?php
$insert= false;
if (isset($_POST['name'])){
//Connection Variables
$server= "localhost";
$username="root";
$password="";
//Create a database connection
$con = mysqli_connect($server,$username,$password);
//Check for Connection success
if(!$con){
die("Connection to this database failed due to".mysqli_connect_error());
}
//Collect Post var
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$phone = $_POST['phone'];
$desc = $_POST['desc'];
$sql= " INSERT INTO `trip`.`trip` (`name`, `age`, `gender`, `email`, `phone`, `desc`, `date`)
VALUES ('$name', '$age', '$gender', '$email', '$phone', '$desc', current_timestamp()); ";
//Execute query
if($con->query($sql)==true){
// echo "Successfully inserted";
$insert= true;
}
else{
echo "Error: $sql <br> $con->error";
}
//Close connection
$con-> close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Roboto|Sriracha&display=swap" rel="stylesheet">
<title>Trip Form</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<img class="bg" src="bg.jpg" alt="Travel">
<div class="container">
<h1>Welcome to Book Karo Apni Trip</h1>
<p>Enter your Details to confirm your participation.</p>
<?php
if($insert==true){
echo "<p class='submitMsg'>Thanks for submitting your form. We are happy to see you joining us for the trip</p> ";
}
?>
<form action="index.php" method="post">
<input type="text" name="name" id="name" placeholder="Enter your name">
<input type="text" name="age" id="age" placeholder="Enter your age">
<input type="text" name="gender" id="gender" placeholder="Enter your gender">
<input type="email" name="email" id="email" placeholder="Enter your email">
<input type="phone" name="phone" id="phone" placeholder="Enter your mobile number">
<textarea name="desc" id="desc" cols="30" rows="10"placeholder="Enter any other information here"></textarea>
<button class="btn">Submit</button>
<!-- <button class="btn">Reset</button> -->
</form>
</div>
<script type="text/javascript">
window.onbeforeunload = function(e) {
document.cookie = 'cookiename=; expires=' + d.toGMTString() + ';';
};
</script>
</body>
</html>
Below is my CSS file:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Roboto', sans-serif;
}
.container{
max-width: 80%;
/* background-color: rgb(229, 145, 229); */
padding:34px;
margin: auto;
}
.container h1,p{
text-align: center;
font-family: 'Sriracha', 'cursive';
font-size: 40px;
}
p{
font-size: 20px;
text-align: center;
font-family: 'Sriracha', 'cursive';
}
.submitMsg{
font-size: 18px;
color: green;
background-color:#ccc;
}
input,textarea{
width: 80%;
margin: 11px auto;
padding:7px;
font-size: 16px;
border: 2px solid black;
border-radius: 6px;
outline: none;
}
form{
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.btn{
color: white;background: purple;
padding: 8px 12px;
font-size: 20px;
border: 2px solid white;
border-radius: 14px;
cursor: pointer;
}
.bg{
position: absolute;
width: 100%;
z-index: -1;
opacity: 0.7;
}
When reloading a page the post data is resent. You may have noticed many bank or e-commerce sites displaying a notice to not reload the page. That is because the action associated with the form is re-processed (they have protection against it though).
So what is happening is everytime you reload your webpage the form is resubmitted. If you have provided the full codes, then each reload will result in a new row being inserted.
There are many ways to allow a reload without re-submission of the data. Some examples are
Redirect user to a different page.
Use ajax to process the submission
Before inserting data to mysql check for duplicate values.
These could be the basic approach. Each of these are very well documents on SO as well as outside.
Suggest you to use an alert() instead. Or you could navigate that page to some other page like LOGIN PAGE.
Use an alert or a toast to indicate a success/error message

PHP posting error

I keep getting a Cannot POST /app/newpost/newpost.php error when i try to post a HTML form and retrieve the data from another HTML page
HTML for the form is:
<div style="text-align:center">
<h1 style="font-size:36px; font-family:verdana;">
Create a new forum post
</h1>
</div>
<form action="newpost.php" method="POST" style="text-align:center">
<P style="font-size:24px; font-family:verdana;">Title: <input type="text" name="title" style="font-size:24px; font-family:verdana;"></P>
<P style="font-size:24px; font-family:verdana;">Description: <input type="text" name="desc" style="font-size:24px; font-family:verdana;"></P>
<input type="submit" action="window.location.href='/app/forum/forum.component.html'" style="background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: left; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer;">
</form>
PHP file is
<?php
if (isset($_POST["submit"]))
{$title = $_POST['title'];
$desc = $_POST['desc']
}
?>
im still very new to PHP someone pls help!
And how to retrieve the posted data on another html page?
You need a web server with PHP installed to test/run your PHP script. You cannot have it run from your file system directory. Depending on your environment you might want to installed something called WAMP, XAMP or MAMP.
Looks like there ; missing
i have changed you code to bellow, it should run now
<?php
if (isset($_POST["submit"]))
{
$title = $_POST['title'];
$desc = $_POST['desc'];
}
?>
try this
I think having action="newpost.php" as an attribute in the form tag and action="window.location.href='/app/forum/forum.component.html'" as an attribute in the input type="submit" tag is a contradiction. You might want to try to erase one of them.

Html form into a Php table

So, im trying to get the informatino you put into an HTML into a Table in Php, problem is, i cant find any solutions online. and i dont know much about php yet. this is only my second time asking something so i still dont quite know the way you are supposed to fill in a question. anyway, here is the code in question:
HTML
</head>
<body>
<form action="http://www.cs.tut.fi/cgi-bin/run~jkorpela/echo.cgi"method="post">
<div>
<label for="vnaam">Voornaam</label>
<input type="text" id="vnaam" name="vnaam" required/>
</div>
<div>
<label for="anaam">Achternaam</label>
<input type="text" id="anaam" name="anaam" required/>
</div>
<div>
<label for="tnum">Telefoon Nummer</label>
<input type="text" id="tnum" name="tnum" />
</div>
<div>
<label for="tnum">E-mail</label>
<input type="email" id="tnum" name="tnum" />
</div>
<div>
<label for="adres">Adres</label>
<input type="" id="adres" name="adres" required/>
</div>
<div>
<label for="land">Land</label>
<select id="land" name="land">
<option value="ned">Nederland</option>
<option value="usa">Amerika</option>
<option value="eng">Engeland</option>
<option value="bel">Belgiƫ</option>
<option value="fr">Frankrijk</option>
<option value="ger">Duitsland</option>
</select>
</div>
<div class="button">
<button type="submit">Opsturen</button>
</div>
</form>
</body>
</html>
Css
<style type="text/css">
form {
/* Just to center the form on the page */
margin: 0 auto;
width: 400px;
/* To see the outline of the form */
padding: 1em;
border: 1px solid #CCC;
border-radius: 1em;
}
form div + div {
margin-top: 1em;
}
label {
/* To make sure that all labels have the same size and are properly aligned */
display: inline-block;
width: 90px;
text-align: right;
}
input, textarea {
/* To make sure that all text fields have the same font settings
By default, textareas have a monospace font */
font: 1em sans-serif;
/* To give the same size to all text field */
width: 300px;
-moz-box-sizing: border-box;
box-sizing: border-box;
/* To harmonize the look & feel of text field border */
border: 1px solid #999;
}
input:focus, textarea:focus {
/* To give a little highlight on active elements */
border-color: #000;
}
textarea {
/* To properly align multiline text fields with their labels */
vertical-align: top;
/* To give enough room to type some text */
height: 5em;
/* To allow users to resize any textarea vertically
It does not work on all browsers */
resize: vertical;
}
.button {
/* To position the buttons to the same position of the text fields */
padding-left: 90px; /* same size as the label elements */
}
button {
/* This extra margin represent roughly the same space as the space
between the labels and their text fields */
margin-left: .5em;
}
</style>
<form method='POST' action='formhandler.php'>
<input type='text' name='name' value='john'>
<input type='text' name='address' value='32 flowers street'>
<input type='text' name='email' value='john#example.com'>
</form>
A typical form like this one will be delivered to the php file formhandler.php by the browser.
Then you handle it in your formhandler.php file
formhandler.php
$name = isset($_POST['name']) ? $_POST['name'] : "";
$address= isset($_POST['address']) ? $_POST['address'] : "";
$email= isset($_POST['email']) ? $_POST['email'] : "";
echo $name; //john
echo $address; //32 flowers street
echo $email; //john#example.com
Now you have the form data. use it as you like, store them in a database, build another html response file to the user or do what ever you want to do with them.
for example, we are going to feedback the data to the user.
echo "We have successfully received your inputs as <br>
<table>
<tbody>
<tr><td>name</td><td>$name</td></tr>
<tr><td>address</td><td>$address</td></tr>
<tr><td>email</td><td>$email</td></tr>
</tbody>
</table>";
Note: in a real life production you need to validate any inputs come to your php files first.
First change
<form action="http://www.cs.tut.fi/cgi-bin/run~jkorpela/echo.cgi"method="post">
to Something like
<form action="http://www.cs.tut.fi/getdata.php" method="post">
Because you are using cgi-bin/run~jkorpela/echo.cgi which is not used for get php data because its a cgi file. it should a .php extension file and php should be installed on your server. and I am assuming that http://www.cs.tut.fi/ is the url for your website.
Where getdata.php is the php file where you get all your input values. You need to create that file on your server. If you are on AWS then it might be something like /var/www/html/
getdata.php
<?php
echo "<pre>";
print_r($_REQUEST);
?>

PHP will not connect with mysql database

Can anyone see a reason why this would be failing to connect with / write to a mysql database? I can't be sure which but I know the data isn't entering when the form is submitted. It just routes to a blank screen.
The html
<html>
<div style="width: 330px; height: 130px; overflow: auto;">
<form STYLE="color: #f4d468;" action="send_post.php" method="post">
Category: <select STYLE="color: #919191; font-family: Veranda;
font-weight: bold; font-size: 10px; background-color: #000000;"
name="category">
<option value="blah1">blah1</option>
<option value="blah2">blah2</option>
<option value="blah3">blah3</option>
<option value="blah4">blah4</option>
</select> <br>
<textarea overflow: scroll; rows="4" cols="60" STYLE="color:
#919191; font-family: Veranda; font-weight: bold; font-size: 10px;
background-color: #000000; width:300px; height:80px; margin:0;
padding:0;" name="contents">
</textarea><br>
<input type="submit" STYLE="color: #919191; font-family: Veranda;
font-weight: bold; font-size: 10px; background-color: #000000;"
value="Create Log">
</form>
</div>
</html>
and send_post.php
<?php
//Connecting to sql db.
$connect=mysqli_connect("localhost","username","password","mysqldatabasename");
//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO mytablename (category, contents)
VALUES ('$_POST[category]', '$_POST[contents]')";
?>
I'm new to this but I researched hard and now I'm at an impasse. I've tried entering code that checks for connection errors but I always just get a white screen and no db updates. I would greatly appreciate help. (Also do the quotes stay included in "localhost", "username" etc?)
Seems to be a simple syntax error.
Missing a closing ) here:
mysqli_query($connect,"INSERT INTO mytablename (category, contents) VALUES ('$_POST[category]', '$_POST[contents]')");
The arguments for the mysqli_connect do need to be enclosed in quotes. You also might want to seriously consider sanitizing your inputs. This form is wide open.

Categories