Calling/Using PHP function in HTML - php

I am practicing with PHP and trying to build Validation for my HTML register form, My idea is to create PHP function for each input field within my HTML form and call it if the field has been left out empty.
Am at a stage where everything is ready but I dont know how to call my functions within HTML...
Could some one sugest a way of calling a functions withing HTML form.
session_start();
if(isset($_POST['reg'])){
$lastname = mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$tel = mysql_real_escape_string($_POST['telephone']);
$firstname = mysql_real_escape_string($_POST['firstname']);
}
class Validation{
public function FirstName(){
if($firstname == NULL){
echo '<p>Enter Name</p>';
}
}
public function LastName(){
if($lastname == NULL){
echo '<p>Enter \surname</p>';
}
}
public function EmailAdress(){
if ($email == NULL){
echo '<p>Enter Email</p>';
}
}
public function TelephoneNumber(){
if($tel == NULL){
echo '<p>Enter Name</p>';
}
}
}
?>
<!-- banner -->
<div class="hero-unit banner-back">
<div class="container">
<div class="row-fluid">
<div class="span8">
</div>
<div class="span4 form-back text-center">
<form name="reg" action="" method="post">
<fieldset>
<legend>Free Workshop</legend>
<div class="input-wrapper">
<input type="text" placeholder="First Name" id="firstname" name="firstname <? FirstName(); ?>"/>
</div>
<div class="input-wrapper">
<input type="text" placeholder="Last Name" id="lastname" name="lastname"/>
</div>
<div class="input-wrapper">
<input type="email" placeholder="Email Address" id="email" name="email"/>
</div>
<div class="input-wrapper telephone">
<input type="text" placeholder="Telephone number" id="telephone" name="telephone"/>
</div>
<div class="input-wrapper">

You cannot call PHP from HTML after you delivered the Website to the Browser. (well you can with AJAX, but that would be some overkill). When the user does some input to the fields the page is not sent to your server (where PHP is interpreted).
So I suggest you do the validation client side in JavaScript.
To output PHP Variables or the return of a Function call simply use the <?= myFunction() ?> syntax.

Related

Data not being posted. Please review my code

I am trying to post the values as shown in the script from an HTML form. I have confirmed that the form is successfully submitting the data. However, when I try to test the values being submitted using the isset() script, I get nothing at all. I am also posting my function to see if I am making an error there.
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// require_once('mystuff/functions.php');
echo $to_account = test_input($_POST['to_account']);
echo $to_email = test_input($_POST['to_email']);
echo $amount = test_input($_POST['amount']);
echo $pin = test_input($_POST['pin']);
} else {
echo "Probably the data was not submitted";
}
<div class="container">
<h2>Transfer:</h2>
<form action="transCtrl.php" method="POST">
<div class="form-group">
<label for="to_account">To Account</label>
<input type="text" class="form-control" id="to_account" name="to_account" required pattern="[A-Z0-9]{13}">
</div>
<div class="form-group">
<label for="to_email">To Email:</label>
<input type="email" class="form-control" id="to_email" name="to_email" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$" required>
</div>
<div class="form-group">
<label for="amount">Amount</label>
<input type="number" class="form-control" id="amount" name="amount" required min="100">
</div>
<div class="form-group">
<label for="pin">8 Digit PIN</label>
<input type="text" class="form-control" id="pin" name="pin" pattern="[0-9]{8}" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
Instead this
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
Use this:
if($_SERVER["REQUEST_METHOD"] == "POST") {
You don't have a $_POST['submit'] variable.

Form posting does not work

I am having trouble to post a html form. I am posting one form and getting the post value to my variable and then I am post this form but this form is not posting.
HTML code:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" autocomplete="off">
<div class="widget-box">
<div class="widget-title"> <span class="icon"> <i class="icon-user"></i> </span>
<h5>Amc details</h5>
</div>
<div class="widget-content">
<div class="controls controls-row">
<div class="control-group span3">
<label for="normal" class="control-label">Installation Date<span style="color:red">*</span></label>
<div class="controls">
<input type="text" id="amc-ins-date" data-date="01-02-2016" name="amc-ins-date" data-date-format="dd-mm-yyyy" class="datepicker span12" placeholder="Enter installation date">
</div>
</div>
<div class="control-group span3">
<label for="normal" class="control-label">Start Date<span style="color:red">*</span></label>
<div class="controls">
<input type="text" id="amc-start-date" data-date="01-02-2016" name="amc-start-date" data-date-format="dd-mm-yyyy" placeholder="Enter amc start date" class="datepicker span12 ins-date">
</div>
</div>
<div class="control-group span3">
<label class="control-label">End Date<span style="color:red">*</span></label>
<div class="controls">
<input type="text" id="amc-end-date" data-date="01-02-2016" name="amc-end-date" data-date-format="dd-mm-yyyy" placeholder="Enter amc end date" class="datepicker span12 ins-date">
</div>
</div>
<div class="control-group span3">
<label class="control-label">Amount<span style="color:red">*</span></label>
<div class="controls">
<input type="text" id="amc-amount" name="amc-amount" class="span12" placeholder="Enter amc amount">
</div>
</div>
</div>
</div>
<div class="form-actions">
<input style="float:right" type="submit" name="amc-installation" class="btn btn-success" value="Save">
</div>
</form>
PHP code:
// i have submitted a form here and its posted
// installation details
$mc_serial = $_POST['mc-serial'];
$mc_model = $_POST['mc-model'];
$contract_type = $_POST['contract_type'];
$no_of_copies = $_POST['no-of-copies'];
$spare_part = join(",",$_POST['spare-part']);
$eng_name = $_POST['eng-name'];
$review = $_POST['review'];
// check if the machine already exits
if(IsMachine($mc_serial,$con)){
echo msgIsMachine();
exit();
}
if($contract_type == 'AMC'){
require './forms/amc.php'; // this is the html i have shown above
} elseif ($contract_type == 'ASC') {
require './forms/asc.php';
} elseif ($contract_type == '4C') {
require './forms/4c.php';
} elseif ($contract_type == 'RENTAL') {
require './forms/rental.php';
} elseif ($contract_type == 'WARRANTY') {
require './forms/warranty.php';
}
if(isset($_POST['amc-installation']) && !empty($_POST['amc-installation'])){
echo "posted";
var_dump($_POST);($_POST);
}
The output of var_dump is NULL. I don't get any problem.
You echo the second form (during the script which responds to the submission of the first one), but then immediately check for values returned from it within the same script execution. You have to wait for the user to post the form back before you can check the submitted values. This would be a separate postback, and therefore a separate execution context for the PHP.
So the code to check the values from the second form needs to be in a separate section (or file) which is triggered by the submission of the second form.
There's no $POST in PHP you should use $_POST instead :
if(isset($_POST['amc-installation']) && !empty($_POST['amc-installation'])){
echo "posted";
var_dump($_POST);
}
NOTE : You should place the var_dump($_POST); inside the if statement, so it will be trrigered just after the submit.
Hope this helps.
if(isset($_POST['amc-installation']) && !empty($_POST['amc-installation'])){
echo "posted";
var_dump($_POST);
}
You should use $_POST not $POST.
Hope this will helps you :)

Problems with PHP form validation

My form is spread across two pages. Pages are turned using the JS slider called Slick. After filling in the form and clicking the send button on page 2, the validation says I didn't fill in my name. You can see this for yourself on this website: Rofordaward. Just complete the nomination form and push send. HTML and PHP code below.
HTML
SNIPPET OF FORM (from nominate.html):
<form action="nominate.php" method="post">
<div class="single-item slider">
<div class="page1">
<label class="row">
<h2 class="headline">Your full name</h2>
<input type="text" name="yourname" placeholder="Forename and surname"></input>
</label>
<label class="row email">
<h2 class="headline">Your email address <p>Don't worry, we won't spam you or share your email address</p></h2>
<input type="text" name="email" placeholder="example#rofordaward.co.uk"></input>
</label>
<label class="row">
<h2 class="headline">Name of company</h2>
<input type="text" name="companyname" placeholder="e.g. Roford"></input>
</label>
</div>
<div class="page2">
<label class="row reason">
<h2 class="headline">Reason for nomination</h2>
<textarea id="textarea" rows="6" cols="25" maxlength="1000" name="reason" placeholder="A brief evidence based summary"></textarea>
<div id="text-area-wrap">
<div id="textarea_feedback"></div>
</div>
</label>
<div class="row button-wrap">
<div class="column small-12">
<input class="button" type="submit" value="Send it!">
</div>
</div>
</div>
</div>
</form>
PHP
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$email = check_input($_POST['email']);
$companyname = check_input($_POST['companyname']);
$reason = check_input($_POST['reason'], "Write your reason");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/*Message for the e-mail */
$message = "New submission
Name: $yourname
E-mail: $email
Company name: $companyname
Reason:
$reason
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
When I remove the scripts for the JS slider, the form becomes fully functional.
In your PHP Code, You are using $_POST['email'] which suits good according to code on your last HTML Block when <input type="text" name="email" /> as you have set it to 'email'
On other hand, in your First HTML Code Block:
<label class="row email">
....
<input type="text" name="youremail" ....></input>
</label>
You set name="youremail", which is definitely going to derive wrong result with your PHP Code. You should be using it like $_POST['youremail'] in your PHP Code to get it working.

codeigniter session user_data being reset

I am using Codeigniter database session. I have a search navigation on the header of my page which contains a search from and to date and a text field.
I also have pagination on the page. I am using a custom controller (MY_Controller) and I am setting the from and to dates here. See below.
public function __construct()
{
parent::__construct();
$this->load->model('News_model');
$this->set_dates();
}
function set_dates()
{
if (empty($this->input->post('from_date')))
{
$this->session->set_userdata('sel_from_date', date('Y-m-d'));
}
else
{
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$this->session->set_userdata('sel_from_date', $this->input->post('from_date'));
}
}
if (empty($this->input->post('to_date')))
{
$this->session->set_userdata('sel_to_date', date('Y-m-d'));
}
else
{
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$this->session->set_userdata('sel_to_date', $this->input->post('to_date'));
}
}
}
This works fine even when I enter dates in the from and/or to fields and click submit, the correct dates stay in the search fields.
However when I click on the link for the next page (in pagination) which basically adds another segment to the URL (i.e. mysite/news/politics/1) and loads the page, the dates in the session user_data are reset. I thought maybe the logic above for blank input dates was being executed again in this instance but that is not the case.
HTML:
<div class="form-group">
<label for="from_date" class="col-sm-2 control-label">From Date:</label>
<div class="col-sm-6">
<input type="text" placeholder="From Date" class="form-control" name="from_date" id="from_date" width=10" value="<?php echo $this->session->userdata('sel_from_date'); ?>" />
</div>
</div>
<div class="form-group">
<label for="to_date" class="col-sm-2 control-label">To Date:</label>
<div class="col-sm-6">
<input type="text" placeholder="To Date" class="form-control" name="to_date" id="to_date" value="<?php echo $this->session->userdata('sel_to_date'); ?>" />
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
<button type="submit" class="btn btn-default" name="go" id="go">Go</button>
</div>
Check whether the session variable 'sel_from_date' is already set .
If yes don't set it again. Try below in your controller:
if($this->session->userdata('sel_from_date') != '')
{
// Do not set the session variable 'sel_from_date'
}
else
{
// Execute the function $this->set_dates()
}
I resolved the issues with the following code.
if (empty($this->input->post('from_date')) && !$this->session->userdata('sel_from_date'))
{
....
}

How to Get Contact Form Working

Okay so I am fairly new to web designing. How do I get the contact form on my current theme to work? This is the current html.
I need to know how to code the PHP file; is this correct?
<div class="form row-fluid clearfix">
<div class="field span5">
<label>Your name:</label>
<input type="text" value="" class="req" placeholder="Placeholder text..." />
</div>
<div class="field span5">
<label>Your email:</label>
<input type="email" value="" class="req" />
</div>
<div class="clearfix"> </div>
<div class="field full">
<label>Your comment:</label>
<textarea class="span12" cols="2" rows="7"></textarea>
</div>
<button class="extruded"><span>Submit</span></button>
</div>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: iclear';
$to = 'sales#tangledindesign.com';
$subject = 'Hello';
?>
And how do I link the PHP file for that contact form?
Step 1
Wrap your fields with a form HTML element that has its action property set to your php processing page
Step 2
Name the form fields according to what the php file expects
Step 3
Add some validation
Step 4
Submit and test
Example
HTML
<form action="process.php" method="post">
First Name: <input type="text" name="first_name">
<input type="submit">
</form>
PHP
<?php
$first_name=$_POST["first_name"];
if($first_name=="John")
{
echo "Hi John!";
}
else
{
echo "Sorry Buddy, Don't really know you";
}
?>
Note
The reason why i did not provide you a full solution is that you mentioned you are a newbie in that programming, and it would be injustice to just solve your problem and not guide you how to do it
You need to wrap your HTML with the tag, and don't forget to get include the submit button:
<form action="process.php" method="post">
<div class="form row-fluid clearfix">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" name="submit">
</div>
</form>
Then here is the php (process.php) file to get all values from your HTML form:
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: iclear';
$to = 'sales#tangledindesign.com';
$subject = 'Hello';
Hope this help.
Try this code
<?php
$toaddress ="youremail#domain.com" //change to your email address
$error ="";
if($_SERVER['REQUEST_METHOD']=="POST") {
$name=$_POST['name'] ;
$email=$_POST['email'] ;
$comment=$_POST['comment'] ;
if(!isset($name) || $name==""){
$error .="Please Enter your name <br/>";
}elseif (!isset($email) || $email==""){
$error .="Please Enter your email Address.<br/>";
}elseif(!isset($comment) || $comment==""){
$error .="Please Enter your Comments.<br/>";
}
if ($error ==""){
mail($toaddress,"Contact form",$comment)
}
}
?>
<?php echo $error ;?>
<form method='post' action='' enctype='multipart/form-data' id='news_form' name='post_form' >
<div class="form row-fluid clearfix">
<div class="field span5">
<label>Your name:</label>
<input name="name" type="text" value="" class="req" placeholder="Placeholder text..." />
</div>
<div class="field span5">
<label>Your email:</label>
<input type="email" value="" class="req" name="email" />
</div>
<div class="clearfix"> </div>
<div class="field full">
<label>Your comment:</label>
<textarea class="span12" cols="2" rows="7" name="comment"></textarea>
</div>
<input type="submit" value="submit" />
</div>
</form>

Categories