unable to insert data into wordpress db using form - php

When i click on submit it doesn't create new posts in the posttype. It just refreshes the page.The below is a pagetemplate assigned to page.
<?php
if(isset($_POST['submit'])){
global $wpdb;
$wpdb->insert( 'contacts', array('post_title'=>$_POST['name'],'email'=>$_POST['email']));
}
?>
<form method="post" action="">
<input type="name" id="inputName" placeholder="Name">
<input type="email" id="inputEmail" placeholder="Email">
<button type="submit">SUBMIT</button>
</form>

You need to add name attribute in your form element
<input type="name" id="inputName" name="name" placeholder="Name">
<input type="email" id="inputEmail" name="email" placeholder="Email">
<button type="submit" name ="submit">SUBMIT</button>

You just need to change the HTML
<form method="post" action="">
<input type="text" id="inputName" placeholder="Name" name="name">
<input type="text" id="inputEmail" placeholder="Email" name="email">
<button type="submit" name="submit">SUBMIT</button>
</form>

<form method="post" action="">
<input type="text" id="inputName" placeholder="Name" name="username">
<input type="text" id="inputEmail" placeholder="Email" name="useremail">
<input type="submit" name="submit" value="submit">
</form>
<?php if(isset($_POST['submit'])){
global $wpdb;
$wpdb->insert( 'contacts', array('post_title'=>$_POST['username'],'email'=>$_POST['useremail']));} ?>
Now Check. I think it's working properly.

Related

Run php file when submit button in another html file

I would like to work on registration process for which with the help of Google and Youtube i have created "Sign-in & Sing-up" page togather with toggle option however unable to run registration.php file once user provide registration info at login.html file. Codes are as follows :
<form id="login" class="input-group">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="checkbox" class="check-box"><span>Remember Password</span>
<button type="submit" class="submit-btn">Sign-In</button>
</form>
<form Id="register" class="input-group">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="email" class="input-field" placeholder="Email Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="password" class="input-field" placeholder="Confirm Password" required>
<input type="phone-number" class="input-field" placeholder="Mobile Number" required>
<input type="checkbox" class="check-box"><span>I agree to the terms & conditions</span>
<button type="submit" class="submit-btn">Sign-Up</button>
</form>
How to execute registration.php file when Sign-up button clicked at login.html file? Same goes for login option too.
Make sure to add method="POST" and action="path/function.php" and name="desiredName"in your forms like this:
<form id="login" class="input-group" method="POST" action="file_path/login.php">
<input type="text" class="input-field" placeholder="User Id" name ="user" required>
<input type="password" class="input-field" placeholder="Enter Password" name="password" required>
<input type="checkbox" class="check-box"><span>Remember Password</span>
<button type="submit" class="submit-btn">Sign-In</button>
</form>
And then in PHP to "catch" the data from the post, you'll use something like this:
$this->getpost['user'];
Or
$_POST['user'];
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").
check here for more details w3schools
<form id="login" class="input-group" method="POST" action="file_path/login.php">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="checkbox" class="check-box"><span>Remember Password</span>
<input type="submit" class="submit-btn" value="Sign-In">
</form>
<form Id="register" class="input-group" method="POST" action="file_path/register.php">
<input type="text" class="input-field" placeholder="User Id" required>
<input type="email" class="input-field" placeholder="Email Id" required>
<input type="password" class="input-field" placeholder="Enter Password" required>
<input type="password" class="input-field" placeholder="Confirm Password" required>
<input type="phone-number" class="input-field" placeholder="Mobile Number" required>
<input type="checkbox" class="check-box"><span>I agree to the terms & conditions</span>
<input type="submit" class="submit-btn" value="Sign-Up">
</form>
EDIT according to your comment
replace button with input type="submit"
and place # before the php variable so you wont get undefined error notice(# is used to avoid error notice)
<div class="header">
<h2>Register here</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo #$username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo #$email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm Password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<label>Mobile number</label>
<input type="number" name="mobile" value="<?php echo #$mobile; ?>">
</div>
<div class="input-group">
<input type="submit" class="btn" name="reg_user" value="Sign-Up">
</div>
</form>
create register.php file
and in register.php
<?php
$con = mysqli_connect("localhost", "username", "password", "dbname") or trigger_error("Unable to connect to the database");
if(isset($_POST['reg_user'])){
$name = $_POST['username']; //here "username" is what you defined in the "name" field of input form
//define other variables
//write your own sql query , here is an example
$query = "INSERT INTO table(name) VALUES(?)";
$stmt = mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt,$query)){
echo "Error";
}else{
mysqli_stmt_bind_param($stmt,"s",$name);
mysqli_stmt_execute($stmt);
}
}
?>

Dynamically change html Login form to Registration form

I have a login form that takes input for user credentials. At the bottom I have a link to a Register user form. It takes me to a whole new page to register a user. However is there a way with JQuery AJAX to change the login form inputs to take registration inputs instead and send to the respective correct processing php files?
My form for login:
<div id="backgroundLogin">
<form name="login" method="post" action="./Login.php">
<fieldset>
<p id="title">
Connect-a-Cutie Login
</p>
<p id="email">
<label> Email:</label>
<input type="text" name="email" placeholder="Type your email...">
</p>
<p id="password">
<label>Password:</label>
<input type="text" name="password" placeholder="Type your password...">
</p>
<button href="./LoginPage.php" type="submit">Submit!</button>
<p>
<p>Not yet a member? Register now!</p>
</fieldset>
</form>
</div>
And my registration form:
<form action="RegistrationPage.php" method="POST">
Name: <input type="text" name="name" placeholder="Enter your name..."/><br/>
Email: <input type="text" name="email" placeholder="Enter your email..."/><br/>
Password: <input type="text" name="password" placeholder="Enter your password..."/> (make sure no one is looking...)<br/>
Addess: <input type="text" name="address" placeholder="Enter your address..."/><br/>
Do you want to be an admin? <input type="text" name="isAdmin" placeholder="TRUE or FALSE"/><br/><br/>
<button type="submit">Submit</button>
</form>
You can use the method of hiding one of the forms.
$("#switchForm").click(function(){
const isLoginVisible = $("#login_form").is(':not(:hidden)');
if(isLoginVisible) {
$("#login_form").hide();
$("#register_form").show();
$("#switchForm > p").html("Already member? Login now!");
} else {
$("#login_form").show();
$("#register_form").hide();
$("#switchForm > p").html("Not yet a member? Register now!");
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="backgroundLogin">
<fieldset>
<form id="login_form" name="login" method="post" action="./Login.php">
<p id="title">
Connect-a-Cutie Login
</p>
<p id="email">
<label> Email:</label>
<input type="text" name="email" placeholder="Type your email...">
</p>
<p id="password">
<label>Password:</label>
<input type="text" name="password" placeholder="Type your password...">
</p>
<button href="./LoginPage.php" type="submit">Submit!</button>
<p>
</form>
<form id="register_form" action="RegistrationPage.php" method="POST" style="display: none;">
Name: <input type="text" name="name" placeholder="Enter your name..."/><br/>
Email: <input type="text" name="email" placeholder="Enter your email..."/><br/>
Password: <input type="text" name="password" placeholder="Enter your password..."/> (make sure no one is looking...)<br/>
Addess: <input type="text" name="address" placeholder="Enter your address..."/><br/>
Do you want to be an admin? <input type="text" name="isAdmin" placeholder="TRUE or FALSE"/><br/><br/>
<button type="submit">Submit</button>
</form>
<a id="switchForm" href="#">
<p>Not yet a member? Register now!</p></a>
</fieldset>
</div>
I think that it will meet your expectations.
Try this! :)
$('.login-form a.show-register-form').click(function(){
$('.login-form').hide();
$('.register-form').show();
});
$('.register-form a.show-login-form').click(function(){
$('.register-form').hide();
$('.login-form').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="backgroundLogin">
<form name="login" method="post" action="./Login.php" class="login-form">
<fieldset>
<p id="title">
Connect-a-Cutie Login
</p>
<p id="email">
<label> Email:</label>
<input type="text" name="email" placeholder="Type your email...">
</p>
<p id="password">
<label>Password:</label>
<input type="text" name="password" placeholder="Type your password...">
</p>
<button href="./LoginPage.php" type="submit">Submit!</button>
<p>
<p>Not yet a member? Register now!</p>
</fieldset>
</form>
<form action="RegistrationPage.php" method="POST" class="register-form" style="display:none;">
Name: <input type="text" name="name" placeholder="Enter your name..."/><br/>
Email: <input type="text" name="email" placeholder="Enter your email..."/><br/>
Password: <input type="text" name="password" placeholder="Enter your password..."/> (make sure no one is looking...)<br/>
Addess: <input type="text" name="address" placeholder="Enter your address..."/><br/>
Do you want to be an admin? <input type="text" name="isAdmin" placeholder="TRUE or FALSE"/><br/>
<button type="submit">Submit</button>
<p>have user?, login here!</p>
</form>
</div>

$_POST array not populated by form submission

I am working on an email subscribe feature on my website. I encountered an issue where the php is not getting the form values. I have looked at numerous threads on this issue and have not had any luck.
So, how can I get the value of the email field in php?
<div>
<form action="/email.php">
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="email">Email</label>
<input type="text" id="email" name="email">
<input type="submit" value="Submit">
</form>
</div>
Email.php
<?php
$email = $_POST["email"];
echo $email;
?>
don't use / before email.php and you forgot method type
<div>
<form action="email.php" method='POST'>
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="email">Email</label>
<input type="text" id="email" name="email">
<input type="submit" value="Submit">
</form>
</div>
add form method Attribute get or post
<form action="/email.php" method = "POST">
Try this way,
HTML:
<form action="email.php" method='POST'>
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="email">Email</label>
<input type="text" id="email" name="email">
<input type="submit" name="save" value="Submit">
</form>
PHP:
<?php
if(isset($_POST["save"]))
{
print_r($_POST);
echo $_POST["email"];
}
?>
This will helpful to you.
Regards
Every thing written perfectly, just fault's in form tag, remove "/" from action and give method="POST" in form tag.
As you are calling that value with post method in email.php, you have to give the post method on form
like this:-
<div>
<form action="email.php" method="POST">
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<label for="email">Email</label>
<input type="text" id="email" name="email">
<input type="submit" value="Submit">
</form>
</div>
rest is all fine
now you can call that value:-
<?php
$email = $_POST["email"];
echo $email;
?>

How to save a form on button submit in php

I am new to php. I am trying to save a form on button submit. But it saves the form when page render. I tried this .
Php Code
<?php
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_catalog_contactus->saveContact($this->request->post);
}
?>
Html Code
<form action="/common/contactus" method="post" class="smart-green">
<h2>Contact Us</h2>
<p><label><span>Name :
</span><input id="name" type="text" name="contactname" placeholder="Your Name">
</label><label><span>Email :</span>
<input id="email" type="text" name="email" placeholder="Your Email">
</label>
<label><span>Mobile :</span>
<input id="email" type="text" name="mobile" placeholder="Your Mobile Number">
</label>
<label><span>Message :</span>
<textarea rows="" cols="" placeholder="Your message here" name="message"></textarea>
</label>
<label><span> </span>
<input type="submit" class="btn btn-primary" value="save" style="margin-top:15px;" name="submit-btn"/>
</label></p>
</form>
Just make sure that form is submitted, like this:
if (!empty($_POST)) {
if ($this->validate()) {
$this->model_catalog_contactus->saveContact($this->request->post);
}
}

Form not submitting data

I have a sign up form in html, but it's not submitting any data to the php file. Weird thing is, to me an exactly the same looking form on the same page does work and this one used to work. I probably somewhere changed something accidently.
The form:
<form action="http://test.com/register.php" method="POST" enctype="multipart/form-data">
<fieldset>
<div align="left">
<label>Username</label>
<input type="text" required class="span11" name="fUsername" id="Username" placeholder="username">
<label>Password</label>
<input type="password" required class="span11" name="fPassword" id="Password" placeholder="password">
<label>Retype password</label>
<input type="password" required class="span11" name="fRetypepassword" id="Retypepassword" placeholder="retype password">
<label>Phone number</label>
<input type="text" required class="span11" name="fPhone" id="Phone" placeholder="+... international format">
<label>E-Mail</label>
<input type="email" required class="span11" name="fEmail" id="Email" placeholder="e-mail">
<br><br>
<button type="submit" class="btn btn-info">Register</button>
</div>
</fieldset>
</form>
The PHP:
$username = htmlspecialchars($_GET['fUsername']);
$password=htmlspecialchars($_GET['fPassword']);
$passwordmatch=htmlspecialchars($_GET['fRetypepassword']);
$email=htmlspecialchars($_GET['fEmail']);
$phone=htmlspecialchars($_GET['fPhone']);
echo $username;
The form is sending data with method POST but you are trying to read it from $_GET.
Try:-
$username = htmlspecialchars($_POST['fUsername']);
$password = htmlspecialchars($_POST['fPassword']);
$passwordmatch = htmlspecialchars($_POST['fRetypepassword']);
$email = htmlspecialchars($_POST['fEmail']);
$phone = htmlspecialchars($_POST['fPhone']);

Categories