I want to check form fields on PHP. I have tried but I don't get any message. I am not sure why.
I use mongodb database, and I'm new in PHP.
My PHP code so far:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post">
<h3> Name :</h3>
<p>
<input type="text" name="username" id="title/">
</p>
<h3>Title :</h3>
<p>
<input type="text" name="title" id="title/">
</p>
<h3>Content :</h3>
<textarea id="content" name="content" rows="20" cols="100"></textarea>
<p>
<br/>
<input type="submit" name="btn_submit" value="Save"/>
</p></form>
</center>
<?php
if (isset($_POST['btn_submit']))
{
$title = $_POST['title'];
$content = $_POST['content'];
if (!isset($title) || empty($title))
{
echo " <h2> check title ! </h2>";
}
if (!isset($content) || empty($content))
{
echo " <h2> check content ! </h2>";
}
}
?>
<?php
else: ?>
<p>
success. _id:<?php echo $article['_id']; ?>.
<a href="blogpost.php">
write other articles ?</a>
</p>
<?php endif; ?>
Just reviewed your code. I think it has a few problems, but the main one is in the if-else sequence.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post">
<h3> Name :</h3>
<p>
<input type="text" name="username" id="title/">
</p>
<h3>Title :</h3>
<p>
<input type="text" name="title" id="title/">
</p>
<h3>Content :</h3>
<textarea id="content" name="content" rows="20" cols="100"></textarea>
<p>
<br/>
<input type="submit" name="btn_submit" value="Save"/>
</p></form>
</center>
<?php
if (isset($_POST['btn_submit']))
{
$title = $_POST['title'];
$content = $_POST['content'];
if (!isset($title) || empty($title))
{
echo " <h2> check title ! </h2>";
}
if (!isset($content) || empty($content))
{
echo " <h2> check content ! </h2>";
}
?>
<?php } else { ?>
<p>
<?php echo $article['_id']; ?>.
<a href="blogpost.php">
write other articles ?</a>
</p>
<?php } ?>
You have syntax error in if else statement and also there is undefined varibale $article['_id']. Where do you get that value from? Other than that the code is fine. Here is the updated code:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post">
<h3> Name :</h3>
<p>
<input type="text" name="username" id="title/">
</p>
<h3>Title :</h3>
<p>
<input type="text" name="title" id="title/">
</p>
<h3>Content :</h3>
<textarea id="content" name="content" rows="20" cols="100"></textarea>
<p>
<br/>
<input type="submit" name="btn_submit" value="Save"/>
</p></form>
</center>
<?php
if (isset($_POST['btn_submit']))
{
$title = $_POST['title'];
$content = $_POST['content'];
if (!isset($title) || empty($title))
{
echo " <h2> check title ! </h2>";
}
else if (!isset($content) || empty($content))
{
echo " <h2> check content ! </h2>";
}
else
{
echo "<p>Success!!! <a href='blogpost.php'>
write other articles ?</a> </p>";
}
}
?>
Related
I'm having problems with a simple contact form.
The error I'm getting is:
Notice: Undefined index: terms in contact.php on line 29
Attention! You have to check the Privacy Policy box to accept our terms.
Code:
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$terms = $_POST['terms']; // <---- line 29
if(trim($terms) == '') {
echo '<div class="alert error"><div class="msg">Attention! You have to check
the Privacy Policy box to accept our terms.</div></div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="alert error"><div class="msg">Attention! Please enter your
message.</div></div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
The template file is below:
<form action="<?php echo $this->config->get('config_url').'ajax/contact.php' ?>" method="post" id="contact_form" enctype="multipart/form-data">
<table>
<tr>
<td>
<label for="name"><?php echo $entry_name; ?></label>
<input type="text" name="name" id="name" style="margin-right:20px" value="<?php echo $name; ?>" />
<?php if ($error_name) { ?>
<span class="error"><?php echo $error_name; ?></span>
<?php } ?>
</td>
<td>
<label for="email"><?php echo $entry_email; ?></label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
<?php if ($error_email) { ?>
<span class="error"><?php echo $error_email; ?></span>
<?php } ?>
</td>
</tr>
</table>
<label for="enquiry"><?php echo $entry_enquiry; ?></label>
<textarea name="enquiry" id="enquiry" cols="30" rows="5"><?php echo $enquiry; ?></textarea>
<?php if ($error_enquiry) { ?>
<span class="error"><?php echo $error_enquiry; ?></span>
<?php } ?>
<input type="checkbox" name="terms" value="<?php echo $terms; ?>" />
<label for="terms">Tick this box to confirm you comply with our Privacy Terms</label>
<input type="submit" id="submit" value="<?php echo $button_continue; ?>" class="button dark-bt" />
</form>
Try this:
$terms = isset($_POST['terms']) ? $_POST['terms'] : '';
You will get the checkbox field only if it is checked by the user in the form.
So, always check like this for checkbox and radio buttons.
<!doctype html>
<?php
This is for setting cookies
//first failed attempt
if (isset($_POST['firstname']) != null || isset($_POST['lastname']) != null ||
isset($_POST['phonenumber']) != null || isset($_POST['email']) != null
|| isset($_POST['sulleyaddress']) != null || isset($_POST['question1']) != null
|| isset($_POST['question2']) != null || isset($_POST['question3']) != null
|| isset($_POST['question4']) != null || isset($_POST['question5']) != null) {
setcookie('firstname',$_POST['firstname']);
setcookie('lastname',$_POST['lastname']);
setcookie('phonenumber',$_POST['phonenumber']);
setcookie('email',$_POST['email']);
setcookie('sulleyaddress',$_POST['sulleyaddress']);
setcookie('question1',$_POST['question1']);
setcookie('question2',$_POST['question2']);
setcookie('question3',$_POST['question3']);
setcookie('question4',$_POST['question4']);
setcookie('question5',$_POST['question5']);
}
this is for reseting cookies
//second failed attempt
if (isset($_POST['firstname']) == null) {
setcookie('firstname','');
}
if (isset($_POST['lastname']) == null) {
setcookie('lastname','');
}
if (isset($_POST['phonenumber']) == null) {
setcookie('phonenumber','');
}
if (isset($_POST['email']) == null) {
setcookie('email','');
}
if (isset($_POST['sulleyaddress']) == null) {
setcookie('sulleyaddress','');
}
if (isset($_POST['question1']) == null) {
setcookie('question1','');
}
if (isset($_POST['question2']) == null) {
setcookie('question2','');
}
if (isset($_POST['question3']) == null) {
setcookie('question3','');
}
if (isset($_POST['question4']) == null) {
setcookie('question4','');
}
if (isset($_POST['question5']) == null) {
setcookie('question5','');
}
?>
<html>
<head>
<title>Assignment 2 - Anthony Taveras</title>
<style>#import url("css/styles.css");</style>
<!-- <link rel="stylesheet" href="styles.css" /> -->
</head>
<body>
<?php
1st step. Make the form appear. Allow user to enter and submit data.
if (!isset($_POST['submit'])) {
?>
<div id="content-container">
<div id="content">
<form name="form" id="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Please Fill Out the Form</legend>
<ol>
<li><input type="text" name="firstname" value="<?php if (isset($_COOKIE['firstname'])) {echo $_COOKIE['firstname'];} ?>" ><label> First Name</label></li>
<li><input type="text" name="lastname" value="<?php if (isset($_COOKIE['lastname'])) {echo $_COOKIE['lastname'];} ?>" ><label> Last Name</label></li>
<li><input type="text" name="phonenumber" value="<?php if (isset($_COOKIE['phonenumber'])) {echo $_COOKIE['phonenumber'];} ?>" ><label> Phone Number</label></li>
<li><input type="text" name="email" value="<?php if (isset($_COOKIE['email'])) {echo $_COOKIE['email'];} ?>" ><label> Email</label></li>
<li><input type="text" name="sulleyaddress" value="<?php if (isset($_COOKIE['sulleyaddress'])) {echo $_COOKIE['sulleyaddress'];} ?>" ><label> Sulley Address</label></li>
</ol>
</fieldset>
<fieldset>
<legend>Please answer these questions</legend>
<ol>
<li class="question"><input type="text" name="question1" value="<?php if (isset($_COOKIE['question1'])) {echo $_COOKIE['question1'];} ?>" ><label> What is your favorite color?</label></li>
<li class="question"><input type="text" name="question2" value="<?php if (isset($_COOKIE['question2'])) {echo $_COOKIE['question2'];} ?>" ><label> Where were you born?</label></li>
<li class="question"><input type="text" name="question3" value="<?php if (isset($_COOKIE['question3'])) {echo $_COOKIE['question3'];} ?>" ><label> What is your favorite food?</label></li>
<li class="question"><input type="text" name="question4" value="<?php if (isset($_COOKIE['question4'])) {echo $_COOKIE['question4'];} ?>" ><label> What is your favorite movie?</label></li>
<li class="question"><input type="text" name="question5" value="<?php if (isset($_COOKIE['question5'])) {echo $_COOKIE['question5'];} ?>" ><label> What is your favorite book?</label></li>
<li><input type="submit" name="submit" value="Submit" /></li>
</ol>
</fieldset>
</form>
</div>
</div>
2nd step once submitted user can view responses
<?php
} elseif (isset($_POST['submit'])) {
?>
<!--=============================================Form Preview--------------------------------------->
<div id="content-container">
<div id="content">
<h1> Here's what you put down</h1>
<p> First Name: <?php print $_POST['firstname']; ?> </p>
<p> Last Name: <?php print $_POST['lastname']; ?> </p>
<p> Phone Number: <?php print $_POST['phonenumber']; ?> </p>
<p> Email: <?php print $_POST['email']; ?> </p>
<p> Sulley Address: <?php print $_POST['sulleyaddress']; ?> </p>
<p class="question"> What is your favorite color? <?php print $_POST['question1']; ?> </p>
<p class="question"> Where were you born? <?php print $_POST['question2']; ?> </p>
<p class="question"> What is your favorite food? <?php print $_POST['question3']; ?> </p>
<p class="question"> What is your favorite movie? <?php print $_POST['question4']; ?> </p>
<p class="question"> What is your favorite book? <?php print $_POST['question5']; ?> </p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="edit" value="Edit" />
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="confirm" value="Finish" />
</form>
</div>
</div>
This is the problem area. This last step of the if else statement will not work properly. I'm not sure why. Any help is needed. Thanks.
<?php } elseif (isset($_POST['confirm'])) {
?>
<!--=============================================Form Confirmed--------------------------------------->
<div id="content-container">
<div id="content">
<p> Thank you, your data has been submitted</p>
</div>
</div>
<?php }
?>
</body>
</html>
You've got two form tags. One has the submit, one the hidden field.
You're not passing the variable therefore.
Try troubleshooting!
echo '<pre>';
print_r($_POST);
I would guess that it is because the forms are empty.
Try:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input id='placeholder' name='placeholder' type='text' value='placeholder' hidden>
<input type="submit" name="confirm" value="Finish" />
</form>
I am trying to add captcha with my custom form in WordPress. so I use the plugin Securimage-WP CAPTCHA for this. the plugin is working fine but in my page where I need this captcha, I used if(is_user_logged_in()) for displaying different forms for logged in or logged out users. it gives me an error Fatal error: Call to undefined function show_form(). please help me to out from this problem. thanks in advance. my current code is below:
<?PHP
/* Template Name: bbb */
get_header();
if(is_user_logged_in()){
//echo "<script> window.location.href='".site_url()."'; </script>";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$values = array();
$errors = array();
$values['name'] = #trim(stripslashes($_POST['contact_name']));
$values['email'] = #trim(stripslashes($_POST['email']));
$values['message'] = #trim(stripslashes(strip_tags($_POST['message'])));
if (empty($values['name'])) $errors['contact_name'] = 'Please enter your name';
if (!preg_match('/^(?:[\w\d-]+\.?)+#(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $values['email'])) $errors['email'] = 'The email address supplied is invalid';
if (strlen($values['message']) < 20) $errors['message'] = 'Please enter a message longer than 20 characters';
if (sizeof($errors) == 0) {
if (function_exists('siwp_check_captcha')) {
// make sure plugin is enabled before calling function
if (false == siwp_check_captcha($err)) {
$errors['captcha'] = $err;
}
}
}
if (sizeof($errors) > 0) {
show_form($errors, $values);
} else {
// form code goes here, no errors & captcha was correct
echo "<span style='font-size: 1.2em'><strong><em>Congrats, you win the captcha solving challenge!</em></strong>";
}
}//if condition end
else {
show_form();
}
?>
<?php function show_form($errors = array(), $values = array()) { ?>
<?php if (sizeof($errors) > 0): ?>
<p>There was a problem with your submission. Please correct the following errors:</p>
<ul>
<?php foreach($errors as $error): ?>
<li><?php echo $error ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
<div>
<label for="contact_name">Your Name:
<input type="text" name="contact_name" id="contact_name" class="input" value="<?php echo htmlspecialchars(stripslashes(#$values['name'])) ?>" size="20" /></label>
</div>
<br />
<div>
<label for="email">E-mail:
<input type="email" name="email" id="email" class="input" value="<?php echo htmlspecialchars(stripslashes(#$values['email'])) ?>" size="25" /></label>
</div>
<br />
<div>
<label for="message">Message:<br /><pre style="border: 0; margin: 0; padding: 0"><textarea name="message" id="message" class="input" rows="8" style="width: 100%"><?php echo htmlspecialchars(stripslashes(#$values['message'])) ?></textarea></pre></label>
</div>
<br />
<?php echo do_shortcode('[siwp_show_captcha]'); ?>
<p> </p>
<p>
<input type="submit" value="Send Message" />
</p>
</form>
<?php }}//user logged in if end
get_footer(); ?>
<?php
function show_form($errors = array(), $values = array()) { ?>
<?php if (sizeof($errors) > 0): ?>
<p>There was a problem with your submission. Please correct the following errors:</p>
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo $error ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
<div>
<label for="contact_name">Your Name:
<input type="text" name="contact_name" id="contact_name" class="input" value="<?php echo htmlspecialchars(stripslashes(#$values['name'])) ?>" size="20" /></label>
</div>
<br />
<div>
<label for="email">E-mail:
<input type="email" name="email" id="email" class="input" value="<?php echo htmlspecialchars(stripslashes(#$values['email'])) ?>" size="25" /></label>
</div>
<br />
<div>
<label for="message">Message:<br /><pre style="border: 0; margin: 0; padding: 0"><textarea name="message" id="message" class="input" rows="8" style="width: 100%"><?php echo htmlspecialchars(stripslashes(#$values['message'])) ?></textarea></pre></label>
</div>
<br />
<?php echo do_shortcode('[siwp_show_captcha]'); ?>
<p> </p>
<p>
<input type="submit" value="Send Message" />
</p>
</form>
<?php } ?>
Would you please move this functions in your current theme functions.php ? And after check it. I hope it's working fine for you.
This code is just refreshing my page. I cant see any errors. Maybe someone else can see the error here in code? I have the same code in two other pages and they do UPDATE.
The code below is in one page:
<body>
<?php
$query=mysqli_connect("localhost","user","","mydb") or die ("Ne moga da se svyrja s bazata danni.");
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['submit']))
{
$datetime=$_POST['datetime'];
$vlekach_teltur=$_POST['vlekach_teltur'];
$driver1=$_POST['driver1'];
$telnomer=$_POST['telnomer'];
$belejka=$_POST['belejka'];
$user=$_POST['user'];
mysqli_set_charset($query,"utf8");
$sql="update teltur set datetime = '$datetime', vlekach_teltur = '$vlekach_teltur', driver1 = '$driver1', telnomer = '$telnomer', belejka = '$belejka', user = '$user' where id='$id'";
$query3 = mysqli_query($query, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);
if(mysqli_query($query3, $sql)){
echo "
<!DOCTYPE html>
<script>
function redir()
{
alert('Успешен запис!');
window.location.assign('index.php');
}
</script>
<body onload='redir();'></body>";
}
else{
echo "Не успешен запис, свържете се с администратора $query3. " . mysqli_error($query);
}
}
mysqli_set_charset($query,"utf8");
$sql2="select * from teltur where id='$id'";
$query1=mysqli_query($query, $sql2);
$query2=mysqli_fetch_array($query1);
?>
<h3 style="text-align:center;">Редакция на телефони Турция</h3>
<form id="docContainer" class="fb-toplabel fb-100-item-column selected- object" enctype="multipart/form-data" method="post" action="">
<div id="section1" class="section">
<div id="column1" class="column ui-sortable">
<div class="fb-grouplabel">
<p>Дата<input type="text" id="datetime" name="datetime" value="<?php echo $query2['datetime']; ?>"/>
<label id="datecheckalert" style="color: red; font-style: italic;"> </label></p>
</div>
<p>Влекач:
<input type="text" name="typeahead" class="typeahead tt-query" autocomplete="on" spellcheck="false" value="<?php echo $query2['vlekach_teltur']; ?>">
</p>
<div id="scrollable-dropdown-menu">
<p>Шофьор:
<input type="text" name="driver1" class="driver1 tt-query" autocomplete="on" spellcheck="false" value="<?php echo $query2['driver1']; ?>">
</p>
</div>
<div id="scrollable-dropdown-menu">
<p>Телефонен номер:
<input type="text" name="telnomer" class="telnomer tt-query" autocomplete="on" spellcheck="false" value="<?php echo $query2['telnomer']; ?>"/>
</p>
</div>
<p>Забележка:
<input type="text" name="belejka" value="<?php echo $query2['belejka']; ?>"/>
</p>
<p>Потребител:
<select id="user" name="user">
<option value="<?php echo $query2['user']; ?>"><?php echo $query2['user']; ?></option>
</select></p>
<p align="center">
<input type="submit" value="ЗАПИС" />
</p>
</div>
</div>
<?php
}
?>
</form>
</body>
I have error reporting as you see but no errors found.
Yeah im so stupid, i missed to name the Submit button.
<input type="submit" value="ЗАПИС" />
Changed to this code: <input type="submit" name="submit" value="ЗАПИС" />
I spend tree hours and after make a post here i found it by my self.
Can some one just delete this thread?
I have a update query that I want to use and it's not working. All data is being posted except for CommentID and I can't understand why.
This is my query's output:
UPDATE comments SET
title='PHP',universitet='Högskolan',
kurs='Objekt orienterad programmering i PHP',
kurskod='HIG480-34', betyg='8', message='kom igen nu PHP'
WHERE CommentID = ''
As you can see WHERE CommentID = '' is empty.
<?php
require_once 'DBConnection/connection.php';
class EditPost{
public $comment;
public $id;
public function __construct() {
$this->comment = comment;
$this->id = mysql_real_escape_string($_GET['CommentID']);
}
public function EditThePost(){
if(!isset($_POST['editComment'])){
$query = "SELECT * FROM comments WHERE CommentID = '$this->id'";
$result = mysql_query($query);
$this->comment = mysql_fetch_array($result);
}elseif(isset($_POST['CommentID'])){
$updateQuery = "UPDATE comments SET title='$_POST[title]',universitet='$_POST[universitet]',kurs='$_POST[kurs]',kurskod='$_POST[kurskod]',betyg='$_POST[betyg]',message='$_POST[TheComment]' WHERE CommentID = '$_POST['CommentID]'";
mysql_query($updateQuery) or die(mysql_error());
echo $updateQuery;
header("Location: loggedin.php");
exit();
}
}
}
Here is the edit page with HTML:
<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
require_once 'DBConnection/connection.php';
require_once 'Posting/editPost.php';
$edit = new EditPost();
$edit->EditThePost();
?>
<!DOCTYPE html>
<html lang="sv">
<?php include('incl/header.php'); ?>
<body>
<!--The Navbar-->
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container" align="center">
Hem ||
<?php include('incl/logoutUser.php'); ?>
</div>
</div>
<!--The page container-->
<div id="container" >
<img src="logo.png" id="logoType" align="center">
<br>
<br>
<span class="label label-warning">Redigera inlägg:</span>
<div class="container" align="left">
<br>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p><span class="label label-info">Titel: </span> <br><input type="text" require name="title" placeholder="Ange titel.." value="<?php echo $edit->comment['title'] ;?>"</p>
<p><span class="label label-info">Högskola: </span> <br><input type="text" require name="universitet" placeholder="Ange högskola.." value="<?php echo $edit->comment['universitet']?>"></p>
<p><span class="label label-info">Kurs: </span> <br><input type="text" require name="kurs" placeholder="Ange kurs.." value="<?php echo $edit->comment['kurs']; ?>"></p>
<p><span class="label label-info">Kurskod: </span> <br><input type="text" require name="kurskod" placeholder="Ange kurskod.." value="<?php echo $edit->comment['kurskod']; ?>"></p>
<p><span class="label label-info">Betyg: </span> <br><input type="text" require name="betyg" placeholder="Betyg mellan 1-10" value="<?php echo $edit->comment['betyg']; ?>"></p>
<p><span class="label label-info">Meddelande: </span></p>
<textarea rows="10" cols="80" require name="TheComment" placeholder="Skriv ditt meddelande.." ><?php echo $edit->comment['message'];?></textarea>
<br><br>
<input type="hidden" name="CommentID" value="<?php echo $_POST['CommentID'];?>"/>
<p><input type="submit" class="btn btn-primary" name="editComment" value="Redigera inlägg"></p>
<br>
</form>
<br />
</div>
</div>
<?php include('incl/footer.php'); ?>
</div>
</body>
</html>
I will answer your question while ignoring the security issues, mostly because I don't have much time right now.
You have one issue in your constructor, where you're assigning the contents of a $_GET['CommentID'] to one variable a the $_POST['CommentID']. This is a really bad idea, you should use either $_GET['CommentID'] or $_POST['CommentID'], using both is asking for trouble.
The reason why your comment ID isn't posting is because it's not in your HTML form. From your link, you are doing
<input type="hidden" name="id" value="<?php echo $_GET['CommentID'];?>"/>
To do what you want, it should read
<input type="hidden" name="CommentID" value="<?php echo $_POST['CommentID'];?>"/>
Change the name attribute of this input to be CommentID, read the contents of $_POST['CommentID'], and your code should work.