How Do I Make My PHP Code Stop Displaying Object Not Found? - php

When I hit the Submit button, I got this instead:
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404.
I've gone through the entire code and I can't seem to spot the bug.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!--super global variables = $_POST & $_SESSION -->
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>User Name: </label><input type="text" name="username" required><br><br>
<label>Secret Word: </label><input type="text" name="secret_word" required><br><br>
<label>Email: </label><input type="text" name="email" required><br><br>
<label>Age: </label><input type="number" name="age" required><br><br>
<label>Full Name: </label><input type="text" name="fullname" required><br><br>
<label>Address: </label><input type="text" name="address" required><br><br>
<label>Costumer Complaint: </label><input type="text" name="cost_comp" required><br><br>
<input type="submit" name="submit" value="submit"><br>
</form>
<?php
//Form validation and sanitization
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$raw_username = trim($_POST["username"]);
$raw_secret_word = trim($_POST["raw_secret_word"]);
$raw_email = trim($_POST["email"]);
$raw_age = trim($_POST["age"]);
$raw_fullname = trim($_POST["raw_fullname"]);
$raw_address = $_POST["raw_address"];
$raw_cost_comp = $_POST["raw_cost_comp"];
$clean_username = filter_var($raw_username, FILTER_SANITIZE_STRING);
$clean_secret_word = filter_var($raw_secret_word, FILTER_SANITIZE_STRING);
$clean_email = filter_var($raw_email, FILTER_VALIDATE_EMAIL);
$clean_age = filter_var($raw_age, FILTER_SANITIZE_STRING);
$clean_fullname = filter_var($raw_fullname, FILTER_SANITIZE_STRING);
function processCustomerQueries($clean_username, $clean_secret_word,
$clean_email, $clean_age, $clean_fullname, $raw_address, $raw_cost_comp) {
if ($clean_username && $clean_secret_word && $clean_email && $clean_age
&& $clean_fullname && $raw_address && $raw_cost_comp) {
echo "Hello Dear " . $clean_username . "<br>";
"Secret Word: " . $clean_secret_word . "<br>";
"Email: " . $clean_email . "<br>";
"Age: " . $clean_age . "<br>";
"Full Name: " . $clean_fullname . "<br>";
"Full Address: " . $raw_address . "<br>";
"Full Address: " . $raw_cost_comp . "<br>";
} else {
echo "Please fill the fields above.";
}
}
}
echo processCustomerQueries($clean_username, $clean_secret_word,
$clean_email, $clean_age, $clean_fullname, $raw_address, $raw_cost_comp);
?>
</body>
</html>

The error looks to be with how you are getting back to the action in the form, try using a blank action,
<!--super global variables = $_POST & $_SESSION -->
<form method="POST" action="">
<label>User Name: </label><input type="text" name="username" required><br><br>
<label>Secret Word: </label><input type="text" name="secret_word" required><br><br>
<label>Email: </label><input type="text" name="email" required><br><br>
<label>Age: </label><input type="number" name="age" required><br><br>
<label>Full Name: </label><input type="text" name="fullname" required><br><br>
<label>Address: </label><input type="text" name="address" required><br><br>
<label>Costumer Complaint: </label><input type="text" name="cost_comp" required><br><br>
<input type="submit" name="submit" value="submit"><br>
</form>

Related

PostgreSQL Data Table will not update from PHP code

I am using postgreSQL, PHP, and an HTML form. The form is a simple scholarship application. My connection script seems to work fine because when I click submit on the HTML form it echos "connected" and doesn't die, however no data from the form is transferred to my table. Please any guidance.
My PHP connection script: connect.php
try {
$dbConn = new PDO('pgsql:host=' . DB_HOST . ';'
. 'port=' . DB_PORT . ';'
. 'dbname=' . DB_NAME . ';'
. 'user=' . DB_USER . ';'
. 'password=' . DB_PASS);
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set error mode to exception
echo “Connected”;
} catch (PDOException $e) {
$fileName = basename($e->getFile(), ".php"); // File that triggers the exception
$lineNumber = $e->getLine(); // Line number that triggers the exception
die("[$fileName][$lineNumber] Database connect failed: " . $e->getMessage() . '<br/>');
}
?>
My PHP submission script: form.php
<?php
require 'connect.php';
$sid = $_POST['sid'];
$firstName = $_POST['fname'];
$preferredName = $_POST['pname'];
$lastName = $_POST['lname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$inSchool = $_POST['inSchool'];
$gDate = $_POST['gDate'];
$gpa = $_POST['gpa'];
$essay = $_POST['essay'];
$submit = $_POST['submit'];
if ($sid = ''){
$query = 'insert into student(firstname,lastname,prefname,address,city,state,zip,phone,email) values (?,?,?,?,?,?,?,?,?)';
$statement = $dbConn->prepare($query);
$statement->execute([$firstname, $lastname,$preferredname,$address,$city,$state,$zip,$phone,$email]);
}
else{
$query = 'insert into student(firstname,lastname,prefname,address,city,state,zip,phone,email) values (?,?,?,?,?,?,?,?,?)';
$statement = $dbConn->prepare($query);
$statement->execute([$firstname, $lastname,$preferredname,$address,$city,$state,$zip,$phone,$email]);
}
$query = 'select sid from student where firstname = ? and lastname = ? limit 1';
$statement = $dbConn->prepare($query);
$statement->execute([$firstname, $lastname,$preferredname,$address,$city,$state,$zip,$phone,$email]);
$results = $statement->fetch();
echo $results[0];
?>
My HTML Form: Application.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
<title>Application</title>
</head>
<body>
<form action="form.php" method="post">
<header>
<img class="logo" src="" alt="logo">
<div>Asterisks Corporation Scholarship Application</div>
</header>
<p class="general">Please fill in all information to apply for the Asterisk's Scholarship.<p>
First Name:
<input type="text" size="15" name="fname" required />
Preferred Name:
<input type="text" size="15" name="pname" >
Last Name:
<input type="text" size="15" name="lname" required />
<p class="newSection">Contact Information</p>
Address:
<input type="text" size="50" name="address" required />
City:
<input type="text" size="15" name="city" required />
State:
<input type="text" size="15" name="state" required />
Zip Code:
<input type="text" size="1" name="zip" placeholder="####" required /><br><br>
Phone Number:
<input type="text" size="10" name="phone" placeholder="(###)-###-###">
Email Address:
<input type="text" size="50" name="email" required />
<p class="newSection">Academic Information</p>
Are you currently enrolled in school?
<input type="checkbox" id="Yes" name="yesBox">
<label for="Yes">Yes</label>
<input type="checkbox" id="No" name="noBox">
<label for="No">No</label><br><br>
What school are you enrolled?
<input type="text" size="50" name="schoolsEnrolled"><br><br>
What is/was your date of Graduation?
<input type="date" name="graduationDate"><br><br>
GPA
<input type="text" size="1" name="gpa">
<p class="newSection">What institutions have you applied?</p>
<input type="text" name="" value="" id="school" name="schoolsApplied">
<button onclick="addToList()" type="button" name="button" id="addButton">Add School</button><br>
<ul id="schoolList"></ul>
<p class="newSection">Please write a small essay as to why you should receive this scholarship and what your plans are after graduation.</p>
<textarea id="essay" style="width: 500px; height: 200px;" alignment="left" onkeyup="wordCounter(),wordsRemaining()" name="essay"></textarea>
<div>
300 Words Minimum & 500 Words Maximum
<div> Word Count: <span id="wordCount">0</span></div>
<div> Words Remaining: <span id="wordsRemaining">0</span></div>
</div>
<p class="newSection">Please confirm each of the following:</p>
<input type="checkbox" id="Transcript" name="transcriptConfirm" required />
<label for="Transcript">I have sent in all of my transcripts</label><br>
<input type="checkbox" id="Schools" name="schoolConfirm" required />
<label for="Schools">All schools that I am considering are in the US</label><br>
<input type="checkbox" id="Awards" name="awardConfirm" required />
<label for="Awards">I understand that the award is $5,000 per year for four years</label><br>
<input type="checkbox" id="Confirm" name="amountConfirm" required />
<label for="Confirm">I have received confirmation that my recommenders have emailed their letters to the Scholarship's Coordinator</label><br><br>
Please type your signature in the text box below: <br><br>
<input type="text" size="20" name="signature" required />
<div><br>
<input type="submit" value ="Submit" name="submit">
<input type="reset" value="Start Over" onclick="MinMax()">
</div>
<script>
function addToList(){
let school= document.getElementById("school").value;
document.getElementById("schoolList").innerHTML += ('<li>'+ school+'</li>');
};
function wordCounter(text){
var count= document.getElementById("wordCount");
var input= document.getElementById("essay");
var text=essay.value.split(' ');
var wordCount = text.length;
count.innerText=wordCount
}
function wordsRemaining(text){
var count= document.getElementById("wordCount");
var input= document.getElementById("essay");
var remaining = document.getElementById("wordsRemaining");
var text=essay.value.split(' ');
var wordCount = text.length;
remaining.innerText=300-wordCount
}
</script>
</form>
</body>
</html>

PHP form not submitting to the next page

The landing page has form but it is not submitting and not redirecting to the next page.After submitting the form, it stays on the same page.
It was alright and was working before but I cant figure out where is the problem.
Code in formPage.php is below:
<form action="insert.php" enctype="multipart/form-data" class="contact_form" method="post" name="htmlform" >
<input class="frm-input" name="name" type="text" size="30" maxlength="50" placeholder="Enter Name" required="required" />
<input class="frm-input" name="email" type="text" size="30" maxlength="80" placeholder="Enter Email" required="required"/>
<input class="frm-input" name="jobtype" type="text" size="30" maxlength="30" placeholder="Job Type" required="required"/>
<input class="frm-input" name="ent_type" type="text" size="30" maxlength="80" placeholder="Entity Type" required="required"/>
<input class="frm-input" name="tas_out" type="text" size="30" maxlength="80" placeholder="Task Outline" required="required"/>
<input class="frm-input" name="l_st" type="text" size="30" maxlength="80" placeholder="Logo style of interest (optional)" />
<textarea required="required" class="frm-input frm-txtarea" name="message" placeholder="Task Description!!" maxlength="1000" cols="25" rows="6" ></textarea>
<input style="float: left;" type="file" name="image" size="66"/>
<input type="submit" class="btn btn-success btn-lg" name="submitt" value="submit" style="float: right" />
</form>
In this file I am trying to get the form information and storing them in database.But this page is not loading after the form submission.
Code in insert.php is below:
<?php
/*
$name = "";
$text = "";
$post = "";
*/
//echo $name;
if (isset($_POST['submitt']))
{
$name = $_POST["name"];
$mail = $_POST["email"];
$j_type = $_POST["jobtype"];
$e_type = $_POST["ent_type"];
$task = $_POST["tas_out"];
$l_st = $_POST["l_st"];
$task_des = $_POST["message"];
$image_name=$_FILES['image']['name'];
$image_type=$_FILES['image']['type'];
$image_size=$_FILES['image']['size'];
$image_temp=$_FILES['image']['tmp_name'];
//$date = date(m-d-y);
echo $name;
echo $mail;
echo $j_type;
echo $e_type;
echo $task;
echo $l_st;
echo $task_des;
if ($image_type=='image/jpeg' || $image_type=='image/png' || $image_type=='image/gif') {
move_uploaded_file($image_temp, "img/$image_name");
}
$connection=mysqli_connect("localhost", "root", "","com");
$query="insert into details (name, mail, j_type, e_type, task_outline, l_style, task_desc, image) values('".$name."','".$mail."','".$j_type."','".$e_type."','".$task."','".$l_st."','".$task_des."','".$image_name."')";
if(mysqli_query($connection,$query)){
//include('test.php');
echo '<h2>Data submitted successfully!!</h2>';
header("refresh:1; url=login.php");
//echo 'Back';
}else{
echo "Data not Submitted!";
# code...
}
}
echo "Data not Submitted!";
?>
echo "Data not Submitted!"; // put this line inside the last bracket
Sorry it was my fault,there was a typo mistake in the form action.Everything else is fine.

PHP power form not sending all fields to email

Im using this php script to send the contents of my form to my email when the user submits the form, but currently im only able to get the last two fields of my form to send in the email.( message and company name are the only two working) How can I get the entire form to send in the email?
<form role="form" method='post' action='backend/php_mailer.php'>
<input type="text" name='name' class="form-control" id="yourname" placeholder="Name">
<input type="email" name='email' class="form-control" id="email" placeholder="Email">
<input type="text" name='phone' class="form-control" id="phone" placeholder="Phone">
<input type="text" name='company' class="form-control" id="company" placeholder="Company Name">
<textarea name='message' class="form-control" id="message" rows="6" placeholder="Message"></textarea>
<button type="submit" class="btn btn-primary btn-lg ">SUBMIT</button>
</form>
<?php
if (isset($_POST['email']))
{
$userName = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'] ;
$company = $_REQUEST['company'] ;
$message = $_REQUEST['message'] ;
mail("peaceuponelove#gmail.com", $subject,
$message,$company,$userName );
echo "Thank you";
}
?>
Each argument of mail() has a specific purpose. You cannot just continually pass in arguments and expect them to be appended to the email. You must use string concatenation.
$message = $_REQUEST['message'] . '</br>' . $email . '<br/>' . $phone . '<br/> ' . $userName . '<br/> . ' $company;
mail("peaceuponelove#gmail.com", $subject,$message);
Sidenote You never declared $subject in the code that you've shown. So here's a subject:
$subject = 'Message from '. $userName .' < ' . $email . ' > ';
Here is what i ended up doing to get all fields in email
<?php
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<input name="name" type="text" value="" size="30"/><br>
<input name="email" type="text" value="" size="30"/><br>
<input name="phone" type="text" value="" size="30"/><br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];
$message=$_REQUEST['message'].$email.$phone;
$subject="$name";
mail("peaceuponelove#gmail.com", $subject, $message);
echo "Email sent!";
?>

Why is my form validation not working?

I'm having troubles validating my form. How do I validate the form using PHP? I've tried lots of different methods and nothing has worked. I can get the inputs to display (although check-box doesn't always display) but it just won't validate.
I also want to display the user's inputs (after it has been validated) onto another page, how do I do that?
Here is my code;
Form:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" value="" required>
<br><br>
<label for="email">Your Email:</label>
<input type="text" name="email" id="email" value="" required>
<br>
<br>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" value="" required>
<br>
<br>
Recipient:
<div>
<label for="admin">
<input type="checkbox" name="recipient[]" id="admin" value="Administrator">
Administrator</label>
<br>
<label for="editor">
<input type="checkbox" name="recipient[]" id="editor" value="Content Editor">
Content Editor</label>
<br>
</div>
<br>
<label for="message">Message:</label>
<br>
<textarea name="message" id="message" cols="45" rows="5" required></textarea>
<input type="hidden" name="submitted" value="1">
<br>
<input type="submit" name="button" id="button" value="Send">
<br>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if ($_POST['submitted']==1) {
if ($_POST['name']){
$name = $_POST['name'];
}
else{
echo "<p>Please enter a name.</p>" ;
}
if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
$email = $_POST['email'];
}
else{
echo "<p>Please enter a valid email.</p>";
}
if ($_POST['subject']){
$subject = $_POST['subject'];
}
else{
echo "<p>Please enter a subject.</p>";
if(empty($_POST['recipient'])){
echo "<p>Please select a recipient</p>";
}else{
for ($i=0; $i < count($_POST['recipient']);$i++) {
echo $_POST['recipient'][$i] . " ";
}
}
}
if ($_POST['message']){
$message = $_POST['message'];
}
/* go to form.php
display results
echo "<strong>Your Name:</strong> ".$name. "<br />";
echo "<strong>Your Email:</strong> ".$email. "<br />";
echo "<strong>Subject:</strong> ".$subject. "<br />";
echo "<strong>Recipient:</strong> ";
echo "<br />";
echo "<strong>Message:</strong> <br /> " .$message;
*/
?>
if (preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/i", $email)
You can't use email in this condition because $email is empty!

Sending a mail with an attachment using php function sendmail

i have a form like this :
this is 1.php
<form action="2.php" method="post">
Name: <input type="text" name="name" /></br>
Email: <input type="text" name="email" />
<input type="submit" value="Submit" />
than 2.php is like this :
<form action="3.php" method="post">
<input type="hidden" name="name" value="<?php echo $_POST['name'];?>"/>
<input type="hidden" name="email" value="<?php echo $_POST['email'];?>"/>
Telephone: <input type="text" name="telephone" /></br>
Location: <input type="text" name="location" />
<input type="submit" value="Submit" />
and the last one 3.php is like this:
<?php
$error = "";
if(isset($_POST['submit'])) {
if(empty($_POST['email']) || empty($_POST['name'])) {
$error .= 'There is an error. Retry. <br />';
}
if(!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$error .= 'E-mail is incorrect. <br />';
}
function sendmail() {
$to = 'user#domain.tld';
$name = "Name : " .$_POST['name'];
$telephone = "Telephone : " . $_POST['telephone']. "";
$location = "Location : " .$_POST['location'];
$subiect = "Form from webpage";
$body = 'form content: '. $name. "\n". $telephone. "\n\n" . $location. "\n\n". $from ;
$from = "From: " . $_POST['email']. "";
if(mail($to,$subiect,$body,$from)) {
echo '<p>Thank you !</p><br />';
}else{
echo '<p>Error with server</p>';
}
}
if($error == "") {
sendmail();
}
echo "<p>".$error."</p>";
}
?>
My question is how can i introduce a <input type="file" name="file" id="file" /> in the 1st page when the user will be able to upload a photo and then on the 3.php page when he will hit the submit button that picture to be in the email
can anyone give me an example please ? Thank you !

Categories