I have a form in a plugin I am creatingin wordpress. The form is a simple test form and it has a hidden field and two text fields. If I enter a number into the text fields, it doesn't process it when I hit submit, it takes to me a page that says
It looks like nothing was found at this location. Maybe try a search?
here is the entire plugin showing the form and the action that processes the form.
<?php
/*
plugin name: deano plugin
description: deano test database to insert data into books table
author: Dean-O
*/
$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path.'/wp-load.php');
function deanoinsertdata() {
/**
* Dean-O database insert book function
*/
global $wpdb;
if(isset($_POST['submitbtn'])){
error_log('I am here');
$data=array(
'wp_id'=>$_POST['wp_id'],
'title'=>$_POST['title'],
'author'=>$_POST['author'],
);
$table_name = 'books';
$foundOne = 1;
error_log('table_name = '.$table_name);
error_log('foundOne = '.$foundOne);
/*$wp_idin = $_POST['wp_id'];
$titlein = $_POST['title'];
$authorin = $_POST['author'];
*/
$wp_idin = $data['wp_id'];
$titlein = $data['title'];
$authorin = $data['author'];
error_log('wp_idin = '.$wp_idin);
error_log('titlein = '.$titlein);
error_log('author = '.$authorin);
/*
see if the record is already in the table
*/
$sql = "select * from books";
print $sql;
$results = $wpdb->get_results($sql);
foreach($results as $result) {
if($result->wp_id==$wp_idin && $result->title==$titlein && $result->author==$authorin)
{
$foundOne = 0;
error_log('foundOne = 0');
}
}
//error_log('logged message');
if($foundOne==1) {
error_log('foundOne = 1 before insert');
$resultinsert = $wpdb->insert($table_name,$data);//, $format=NULL);
error_log('insert executed');
error_log('resultinsert = '.$resultinsert);
//wp_redirect( "http://localhost/tadpolewp/deano-plugin--duplicate-records/" );
//exit();
if($resultinsert==1) {
//header('Location: http://localhost/tadpolewp/deano-plugin-successful/');
error_log( 'successful' );
wp_redirect( "http://localhost/tadpolewp/deano-plugin-successful/" );
exit();
http://localhost/tadpolewp/deano-plugin-successful/
//error_log('Book saved 1');
//echo "Book Saved 1";
} else {
//header('Location: http://localhost/tadpolewp/deano-plugin-failed/');
error_log( 'failed to save' );
wp_redirect( "http://localhost/tadpolewp/deano-plugin-failed/" );
exit();
//error_log('unable to save');
//echo "Unable to Save";
}
} else {
//error_log('Duplicate record found');
//echo "Duplicate recortd found";
//header('Location: http://localhost/tadpolewp/deano-plugin-duplicate-records/');
error_log( 'duplicate record' );
wp_redirect( "http://localhost/tadpolewp/deano-plugin-duplicate-records/" );
exit();
}
}
?>
<form role="form" method="post">
<div class="form-group">
<?php
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
?>
<input type="hidden" name="wp_id" value="<?php echo esc_attr( $current_user_id ); ?>" />
</div>
<div class="form-group">
<label>Field 1</label><br>
<input id="title" name="title" type="text" placeholder="<?php echo esc_attr( $current_user_id ); ?>" required="">
</div>
<div class="form-group">
<label>Field 2</label><br>
<input id="author" name="author" type="text" placeholder="Primary Author" required="">
</div>
<div class="row justify-content-center">
<div class="col-xs-4 col-sm-4 col-md-4">
<br><input type="submit" value="Submit1" class="btn btn-info btn-block" name="submitbtn">
</div>
</div>
</form>
<?php
}
add_shortcode('deanoputdatain','deanoinsertdata');
?>
The only way I can get the Field 1 or Field 2 to take numbers is to change them to type="number"
Is there a varchar type that I can use?
My database has the field set as a varchar.
Thanks in advance
Dean-O
You should be set action for your form.
For example: 'test.php' or '/'.
It worked well for me. I rewrite your code here:
<form role="form" method="post" action="{your menu slug}">
<div class="form-group">
<?php
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
?>
<input type="hidden" name="wp_id" value="<?php echo esc_attr( $current_user_id ); ?>" />
</div>
<div class="form-group">
<label>Field 1</label><br>
<input id="title" name="title" type="text" placeholder="<?php echo esc_attr( $current_user_id ); ?>" required="">
</div>
<div class="form-group">
<label>Field 2</label><br>
<input id="author" name="author" type="text" placeholder="Primary Author" required="">
</div>
<div class="row justify-content-center">
<div class="col-xs-4 col-sm-4 col-md-4">
<br><input type="submit" value="Submit1" class="btn btn-info btn-block" name="submitbtn">
</div>
</div>
</form>
Related
I want to send the value of disabled checkbox into the database. I tried to set it to readonly but it is allowing user to check/uncheck the field.
<?php
if(isset($_POST['bookbtn'])){
if(!empty($_POST['checky'])) {
$lang = implode(",",$_POST['checky']);
// Insert and Update record
$checkEntries = mysqli_query($conn,"SELECT * FROM seats");
if($test == $hotel)
$ic = "update seats set seat='$lang' where hotelname='$test' ";
mysqli_query($conn,$ic);
}else{
$del = "insert into seats (hotelname,seat) values ('$test','$lang')";
mysqli_query($conn,$del);
}
}
// code for checkbox
<form method="post">
<div><input type="submit"class="button1" name="bookbtn" value="Book Your table" onclick="bookbtn()"/>
<input type="reset" class="button1"name="resetboxes"value="Reset"></div><br><br><div id="mask2"style="float:left;width:20%;">
<?php $i=1;
while ($i<=$tab)
{?>
<div class="TWO">
<div class="check"style="height:40px;width:120px;">
<div class="seconda">
</div>
<div class="secondb">
<input type ="checkbox"name="checky[]"id="<?php echo "two".$i;?>"class="tabtwo"style="width:30px;height:30px;"
value="<?php echo "two".$i;?>"
<?php if (in_array("two".$i, $expcheck)) {?>
checked="checked"<?php }else {echo "none";}?>disabled>
</div>
<div class="secondc">
</div>
</div>
</div>
<?php
$i++;
}
?>
try this
<input type="checkbox" onclick="this.checked=this.defaultChecked" />
<input type="checkbox" checked onclick="this.checked=this.defaultChecked" />
I started coding my website that we are gonna use to run business with my friend but i dont know how to connect my Login/Register page to mySQL database(running on xampp control panel with Apache server and MySQL server).
Any solution would be helpful. Thank you.
Login:
<link rel="stylesheet" type="text/css" href="loginstyle.css">
<form action="action_page.php">
<div class="container">
<label for="uname"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<button type="submit">Login</button>
<label>
<input type="checkbox" checked="checked" name="remember"> Remember me
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot password?</span>
</div>
</form>
Register:
<link rel="stylesheet" type="text/css" href="registerstyle.css">
<form action="action_page.php">
<div class="container">
<h1>Register</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<label for="email"><b>Email</b></label>
<input type="text" placeholder="Enter Email" name="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<label for="psw-repeat"><b>Repeat Password</b></label>
<input type="password" placeholder="Repeat Password" name="psw-repeat" 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>
You will need to add PHP
Connect.php
<?php
/* Database connection settings */
$host = '';
$user = '';
$pass = '';
$db = '';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
?>
Add This to a different page with the extension to .php and Change action File to Login.php
Login.Php
session_start();
include("db.php");
if (isset($_POST['uname']) && isset ($_POST['pse'])) {
$username = $mysqli->escape_string($_POST['pse']);
$result = $mysqli->query("SELECT * FROM TABLE WHERE username='$username'");
if ( $result->num_rows == 0 ){
$_SESSION['message'] = "User with that email doesn't exist!";
echo '<script language="javascript">';
echo 'alert("'.$_SESSION['message'].'")';
echo '</script>';
}
else {
$user = $result->fetch_assoc();
if ( password_verify($_POST['Password'], $user['password']) ) {
$_SESSION['email'] = $user['email'];
$_SESSION['active'] = $user['active'];
$_SESSION['logged_in'] = true;
header("location: System/index.php");
}
else {
$_SESSION['message'] = "You have entered wrong password!";
echo '<script language="javascript">';
echo 'alert("'.$_SESSION['message'].'")';
echo '</script>';
}
}
}
?>
Add This to a different page with the extension to .php and Change action File to Register.php
Register.php
<?php
session_start();
include("db.php");
if (isset($_POST['email']) && isset ($_POST['psw']) && isset ($_POST['psw-repeat'])){
$email = $mysqli->escape_string($_POST['email']);
$password = $mysqli->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
$password-repeat= $mysqli->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
$result = $mysqli->query("SELECT * FROM TABLE WHERE email='$email'") or die($mysqli->error());
if ( $result->num_rows > 0 ) {
$_SESSION['message'] = 'User with this email already exists!';
header("location: error.php");
}
if( $password !== $password-repeat){
$_SESSION['message'] = 'Password dont match';
header("location: error.php");
}
else {
$sql = "INSERT INTO TABLE( email, password)"
. "VALUES ('$first_name','$last_name','$email','$password', '$hash')";
header("location: index.php");
}
?>
my php function is not working when im add my text area in form. other all input or working. if im add remark(text area) in form edit function cant show text in that remark. other input is visible but i cant save it again
please help me to fined my error
Code before "html"
<?php
function renderForm($id, $vehicle_type, $duration, $amount,$remarks, $error)
{
?>
my HTML form
<form action="" method="post">
<div class="row">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div class="col-lg-4 col-xs-6" class="form-group">
<label>Vehicle Type <span style="color:red;font-size:8px;"><i class="fa fa-asterisk" aria-hidden="true"></i></span></label>
<select name="vehicle_type" class="form-control">
<option <?php echo ($vehicle_type=='Bicycle')?'selected':'' ?>>Bicycle</option>
<option <?php echo ($vehicle_type=='Bike')?'selected':'' ?>>Bike </option>
<option <?php echo ($vehicle_type=='Cars')?'selected':'' ?>>Cars </option>
<option <?php echo ($vehicle_type=='Truck')?'selected':'' ?>>Truck</option>
<option <?php echo ($vehicle_type=='Others')?'selected':'' ?>>Others</option>
</select>
</div>
<div class="col-lg-4 col-xs-6" class="form-group">
<label>Duration </label> <span style="color:red; font-size:8px; "><i class="fa fa-asterisk" aria-hidden="true"></i></span>
<input type="text" value="<?php echo $duration; ?>" name="duration" class="form-control" maxlength="20" placeholder="Eg: 4 Hrs">
</div>
<div class="col-lg-4 col-xs-6" class="form-group">
<label><i class="fa fa-inr" aria-hidden="true"></i> Amount</label> <span style="color:red;font-size:8px;"><i class="fa fa-asterisk" aria-hidden="true"></i></span>
<input type="number" name="amount" value="<?php echo $amount; ?>" class="form-control" placeholder="00">
</div>
</div>
<div class="row">
<div class="col-lg-4 col-xs-6" class="form-group">
<label>Remarks</label>
<textarea class="form-control" name="remarks" <?php echo htmlspecialchars($remarks); ?> rows="3" placeholder="Enter ..."></textarea>
</div>
<div id="butn" class="col-lg-3 col-xs-3">
<button class="myButton" type="submit" name="submit" value="Submit" class="btn btn-block btn-success btn-lg">SAVE</button>
</div>
</div>
</form>
Code after "html"
<?php
}
// connect to the database
include('connection.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$vehicle_type = mysql_real_escape_string(htmlspecialchars($_POST['vehicle_type']));
$duration = mysql_real_escape_string(htmlspecialchars($_POST['duration']));
$amount = mysql_real_escape_string(htmlspecialchars($_POST['amount']));
$remarks = mysql_real_escape_string(htmlspecialchars($_POST['remarks']));
if ($vehicle_type=='' || $duration=='' || $amount=='' || $remarks=='')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($id, $vehicle_type, $duration, $amount, $remarks, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE price_normal SET vehicle_type='$vehicle_type', duration='$duration', amount='$amount', remarks='$remarks', WHERE id='$id'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: pnormal.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM price_normal WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$vehicle_type = $row['vehicle_type'];
$duration = $row['duration'];
$amount = $row['amount'];
$remarks = $row['remarks'];
// show form
renderForm($id, $vehicle_type, $duration, $amount, $remarks,'');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
Take a look at the actual HTML in your browser. You're rendering the "remarks" content as an attribute of the textarea element:
<textarea class="form-control" name="remarks" <?php echo htmlspecialchars($remarks); ?> rows="3" placeholder="Enter ..."></textarea>
It should be the content of that element:
<textarea class="form-control" name="remarks" rows="3" placeholder="Enter ..."><?php echo htmlspecialchars($remarks); ?></textarea>
i have a bootstrap one page with a contact form in the end. I would like on submission to focus on that section and not at the beginning of the page.
So, if the message was send, the users will see the OK message, else they will read the error.
here is my code.
Html
<section id="contact">
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 class="heading">Contact</h2>
<div class="row">
<div class="col-md-12">
<?php
$to = 'mail#mail.mm';
$subject = 'Enquiry from the website';
$contact_submitted = 'Your message was submitted and will be responded to as soon as possible. Thank you for contacting us.';
function email_is_valid($email) {
return preg_match('/^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
}
if (!email_is_valid($to)) {
echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
}
if (isset($_POST['contact_submitted'])) {
$return = "\r";
$youremail = trim(htmlspecialchars($_POST['your_email']));
$yourname = stripslashes(strip_tags($_POST['your_name']));
$yourmessage = stripslashes(strip_tags($_POST['your_message']));
$contact_name = "Name: ".$yourname;
$message_text = "Message: ".$yourmessage;
$user_answer = trim(htmlspecialchars($_POST['user_answer']));
$answer = trim(htmlspecialchars($_POST['answer']));
$message = $contact_name . $return . $message_text;
$headers = "From: ".$youremail;
if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourmessage != "" && substr(md5($user_answer),5,10) === $answer) {
mail($to,$subject,$message,$headers);
$yourname = '';
$youremail = '';
$yourmessage = '';
echo '<p style="color: blue;">'.$contact_submitted.'</p>';
}
else echo '<p style="color: red;">Please enter your name, a valid email address, your message and the answer to the simple maths question before sending your message.</p>';
}
$number_1 = rand(1, 9);
$number_2 = rand(1, 9);
$answer = substr(md5($number_1+$number_2),5,10);
?>
<form id="contact" action="contact.php" method="post">
<div class="form_settings">
<div class="form-group">
<label for="Name :">Your firstname *</label>
<input type="text" name="your_name" placeholder="Enter your firstname" required="required" class="form-control">
</div>
<div class="form-group">
<label for="Email Address :">Your email *</label>
<input type="email" name="your_email" placeholder="Enter your email" required="required" class="form-control">
</div>
<div class="form-group">
<label for="Message :">Your message for us *</label>
<textarea rows="4" name="your_message" placeholder="Enter your message" required="required" class="form-control"></textarea>
</div>
<p style="line-height: 1.7em;">To help prevent spam, please enter the answer to this question :</p>
<p><span><?php echo $number_1; ?> + <?php echo $number_2; ?> = ?</span><input type="text" name="user_answer" /><input type="hidden" name="answer" value="<?php echo $answer; ?>" /></p>
<p style="padding-top: 15px"><span> </span><input class="submit" type="submit" name="contact_submitted" value="send" /></p>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
Thank you very much in advance.
Use AJAX in the html to call php.
INSERT INTO `test`.`lecturer` (`lecturerID`, `course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`) VALUES (NULL, NULL, 'test', 'test', 'a#gmail.com', '123456');
Provided was the test code I keyed in into my phpmyadmin, lecturerID was the primary key and also had auto increment on, by my understanding i wasnt suppose to key in anything there and the sql server will input the next number for me, however I keep getting #1062 - Duplicate entry '0' for key 'PRIMARY'. Anyone maybe can give me some insight why am i facing this? Thanks.
Full Code:
<?php include_once "/_core/core.php"; ?>
<?php
if(isset($_POST['id']))
{
$delid = mysql_real_escape_string($_POST['id']);
$sql="DELETE FROM lecturer WHERE lecturerID='$delid'";
mysql_query($sql);
}
if(isset($_POST['fname']) and ($_POST['lname']) and ($_POST['email']) and ($_POST['password']))
{
$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
echo $fname, $lname, $email, $password;
$sql = 'INSERT INTO lecturer (lecturerID, course_ID, lecturerFirstName, lecturer_LastName, lectureremail, lecturerpassword) VALUES (NULL,"0","'.$fname.'","'.$lname.'","'.$email.'","'.$password.'")';
mysql_query($sql);
}
?>
<?php
if(!empty($_SESSION['Admin']) && !empty($_SESSION['Username']))
{
?>
<?php include "_core/menubar.php"; ?>
<br><br><br>
<form class="form-horizontal" action="add_lecturer.php" method="POST">
<fieldset>
<!-- Form Name -->
<legend>Add lecturer</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="fname">First Name</label>
<div class="col-md-4">
<input id="fname" name="fname" type="text" placeholder="First Name" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="lname">Last Name</label>
<div class="col-md-4">
<input id="lname" name="lname" type="text" placeholder="Last Name" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="email">Email</label>
<div class="col-md-5">
<input id="email" name="email" type="text" placeholder="test#apu.edu.my" class="form-control input-md" required="">
</div>
</div>
<!-- Password-->
<div class="form-group">
<label class="col-md-4 control-label" for="password">Password</label>
<div class="col-md-4">
<input id="password" name="password" type="password" placeholder="*****" class="form-control input-md" required="">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" class="btn btn-success">Submit</button>
</div>
</div>
</fieldset>
</form>
<?php
$query = ('SELECT * FROM lecturer');
$q = mysql_query($query);
if(mysql_num_rows($q) > 0)
{
?>
Lecturer List
<table class="table table-striped table-hover">
<form action="add_lecturer.php" method="post">
<?php
while($row = mysql_fetch_array($q)) {
echo "<tr>";
echo "<td>" . $row['lecturerID'] . "</td>";
echo "<td>" . $row['lecturerFirstName'] . "</td>";
echo "<td>" . $row['lecturerLastName'] . "</td>";
echo "<td>" . $row['lectureremail'] . "</td>";
echo '<td><input type="submit" name="deleteItem" value="Delete" /></td>';
echo '<td><input type="hidden" id="id" name="id" value="'.$row['lecturerID'].'" /></td>';
echo "</tr>";
}
?>
</table>
</form>
<?php
}
else
{
echo "You have no lecturers added!";
}
?>
<?php
}
else
{
echo "<meta http-equiv='refresh' content='2;index.php' />";
?>
<?php include "../_class/login.php"; ?>
<?php
}
?>
</div>
<?php include "../_class/footer.php"; ?>
You have to pass NULL for auto increment column not Blank value like this
INSERT INTO `test`.`lecturer`
(`lecturerID`, `course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`)
VALUES (NULL, NULL, 'test', 'test', 'a#gmail.com', '123456');
You should assign NULL value to that auto increment column:
INSERT INTO `test`.`lecturer`
(`lecturerID`, `course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`)
VALUES
(NULL, NULL, 'test', 'test', 'a#gmail.com', '123456');
Or you can try insert without specifying that column in your query as follows:
INSERT INTO `test`.`lecturer`
(`course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`)
VALUES
(NULL, 'test', 'test', 'a#gmail.com', '123456');
INSERT INTO `test`.`lecturer` (`lecturerID`, `course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`)
VALUES ('', '', 'test', 'test', 'a#gmail.com', '123456');
you must have your assign key auto increment in sql database