Form not submitting when I load it with ng-view - php

I have configured the angularjs file so that the view is loaded when i press on the 'add item' button in the navigation bar, however when I try submitting the form nothing happens. But if I load the php file separately it works without any problems and adds the data to my json file.
MY HTML
<head>
<title></title>
<link href="almstyle.css" type="text/css" rel="stylesheet">
<link href="Framework/css/mycss.css" type="text/css" rel="stylesheet">
<link href="Framework/css/bootstrap.min.css" type="text/css" rel="stylesheet">
<script src="Framework/js/jquery-3.1.1.min.js"></script>
<script src="Framework/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<!-- Start of header -->
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<h4 id="banner">LOGO</h4>
</div>
</div>
<!-- End of header -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header navbar-left">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Home <span class="sr-only">(current)</span></li>
<li>Hair</li>
<li>Shop</li>
<li>News</li>
<li>Add Item</li>
<li class="dropdown">
LifeStyle <span class="caret"></span>
<ul class="dropdown-menu">
<li>Food</li>
<li>Passion</li>
<li>Travel</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div data-ng-view></div>
</div>
<script type="text/javascript" src="https://code.angularjs.org/1.6.5/angular.js"></script>
<script type="text/javascript" src="../node_modules/angular-route/angular-route.js"></script>
<script src="app.js"></script>
<script src="home/home.js"></script>
<script src="addItem/addItem.js"></script>
</body>
</html>
My Php Template
<?php
$message = '';
$error = '';
if(isset($_POST["submit"]))
{
if(empty($_POST["item"]))
{
$error = "<label class='text-danger'>Enter Item Name</label>";
}
else if(empty($_POST["id"]))
{
$error = "<label class='text-danger'>Enter Item Id</label>";
}
else if(empty($_POST["size"]))
{
$error = "<label class='text-danger'>Enter a size for your item</label>";
}
else if(empty($_POST["price"]))
{
$error = "<label class='text-danger'>Enter a suitable price for the item</label>";
}
else
{
if(file_exists('items.json'))
{
$current_data = file_get_contents('items.json');
$array_data = json_decode($current_data, true);
$extra = array(
'item' => $_POST['item'],
'id' => $_POST["id"],
'selected' => $_POST["selected"],
'prices' => [[
'size' => $_POST["size"],
'price' => $_POST["price"]
]]
);
$array_data[] = $extra;
$final_data = json_encode($array_data);
if(file_put_contents('items.json', $final_data))
{
$message = "<label class='text-success'>The Item has been added successfully</p>";
}
}
else
{
$error = 'JSON File does not exist';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add items</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container" style="width:500px;">
<h3 align="">Add items</h3><br />
<form method="post">
<?php
if(isset($error))
{
echo $error;
}
?>
<br />
<label>Item Name</label>
<input type="text" name="item" class="form-control" /><br />
<label>Item Id</label>
<input type="text" name="id" class="form-control" />
<input type="hidden" name="selected" value="0" class="form-control" hidden="hidden"/><br />
<div class="panel panel-default">
<div class="panel panel-heading">
Prices
</div>
<div class="panel panel-body">
<label>Size</label>
<input type="text" name="size" class="form-control" />
<label>Price</label>
<input type="number" name="price" class="form-control" />
</div>
<br />
</div>
<input type="submit" name="submit" value="Add Item" class="btn btn-info" /><br />
<?php
if(isset($message))
{
echo $message;
}
?>
</form>
</div>
<br />
</body>
This is what it should look like after submitting

few mistakes here ,
I don't see a form with the action attribute set as path to php resource.
For a form to submit, you should have a button of type submit. Some thing like this.
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
And your PHP file can be
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
I hope this helps you to resolve this

For a form to work "the angular way", you need a couple to specify a couple of extra bits.
You'll firstly need the name the ng-app after the opening html tag to indicate that the that block of code will be 'watched' by an angular module (which you will specify also in the code).
You'll also need to add an ng-controller tag to the form which you will name and then refer to in your javascript
In the angular form , add an ng-click to send it to submit the form if the form validates successfully: ng-click=”formsubmit(userForm.$valid)”
Here is some sample code:
<!DOCTYPE html>
<html ng-app="formExample">
<head>
<title>simple form with AngualrJS</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/angular.js" ></script>
<script src="js/formjs.js"></script>
</head>
<body>
<div ng-controller="formCtrl">
<form name="userForm" class="well form-search" >
<input type="text" ng-model="name" class="input-medium search-query" placeholder="Name" required >
<input type="email" ng-model="email" class="input-medium search-query" placeholder="Email" required >
<input type="text" ng-model="message" class="input-medium search-query" placeholder="Message" required >
<button type="submit" class="btn" ng-click="formsubmit(userForm.$valid)" ng-disabled="userForm.$invalid">Submit </button>
</form>
<pre ng-model="result">
{{result}}
</pre>
</div>
</body>
</html>
JS:
<pre class="lang:default decode:true " title="formjs.js" >
/**
* #filesource : formjs.js
* #author : Shabeeb <mail#shabeebk.com>
* #abstract : controller fo HTML page
* #package sample file
* #copyright (c) 2014, Shabeeb
* shabeebk.com/blog
*
* */
var app = angular.module('formExample', []);
app.controller("formCtrl", ['$scope', '$http', function($scope, $http) {
$scope.url = 'submit.php';
$scope.formsubmit = function(isValid) {
if (isValid) {
$http.post($scope.url, {"name": $scope.name, "email": $scope.email, "message": $scope.message}).
success(function(data, status) {
console.log(data);
$scope.status = status;
$scope.data = data;
$scope.result = data;
})
}else{
alert('Form is not valid');
}
}
}]);</pre>
submit:
<?php
/**
* #filesource : submit.php
* #author : Shabeeb <mail#shabeeb.com>
* #abstract : simple submission php form
* #package sample file
* #copyright (c) 2014, Shabeeb
*
*
* */
$post_date = file_get_contents("php://input");
$data = json_decode($post_date);
//saving to database
//save query
//now i am just printing the values
echo "Name : ".$data->name."n";
echo "Email : ".$data->email."n";
echo "Message : ".$data->message."n";
?>

Related

Success message pop-up after from submit button clicked

I have created a web php web page with html and php code in, see below.
When I submit my form, it sends me an email and a success of fail message appears. So, everything works well.
My problem: I want to make the success or failure message pop-up or be a modal, but I don't know how. Where should I change the code and add the modal?
I have php at the top and then my HTML form. My form also has the google recaptcha verification tool. I just want to add a modal, but I do not know how and where to add the code.
<?php
error_reporting(0);
$msg="";
if (isset($_POST['submit'])) {
$to = "aleciadeklerk.119#gmail.com";
$subject = "Form Submission";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$check = $_POST['check'];
$msgBody = 'Name: '.$name.'Message: '.$message.'Subscribe: '.$check.'E-mail: '.$email;
$headers = 'From: '.$email;
$secretKey = "6LdXbq8UAAAAAM1B79Yz2IPgcTuIynBXeJMF2ZLY";
$responseKey = $_POST['g-recaptcha-response'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response->success) {
if (mail($to, $subject, $msgBody, $headers)) {
$msg="Message sent successfully!";
}
else {
$msg="Failed to send the message. Please try again.";
}
}
else {
$msg="Verification Failed";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.7.0/css/all.css' integrity='sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ' crossorigin='anonymous'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="style.css">
<title>Dibene Solutions</title>
<link rel = "icon" type = "image/png" href = "images/dibene_icon_dark.png">
</head>
<body>
<div>
<div class="container-fluid" id="contact">
<div class="container">
<div class="row">
<div class="col-md-12">
<a name="contact"><h2>Contact us</h2></a>
</div>
</div>
<div class="row">
<div class="col-md-6">
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" class="p-2">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Enter name" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Enter e-mail" required>
</div>
<div class="form-group">
<textarea name="message" rows="4" class="form-control" placeholder="Write your message" required></textarea>
</div>
<div class="form-group">
<label><input type="checkbox" name="check" class="form-control" checked>Subscribe to monthly newsletter.</label>
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="6LdXbq8UAAAAAAf7mQJqfbBbLWA36c1Qiin8EhBp"></div>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Send" class="btn btn-block">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
My aim is to click the submit button and then a modal pops up with a success or failure message.
if you want to use modal as an information messages, put your modal inside your html after the body tag and then call it with a javascript after the event success or failed.
javascript used for modal: $('#myModal').modal(options).
or if you want simplier way, you can use javascript alert() to inform the success or failed event
I made few changes to the code. I included bootstrap modal code and javascript function to call pop up. check this code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.7.0/css/all.css' integrity='sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ' crossorigin='anonymous'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="style.css">
<title>Dibene Solutions</title>
<link rel = "icon" type = "image/png" href = "images/dibene_icon_dark.png">
</head>
<body>
<div>
<div class="container-fluid" id="contact">
<div class="container">
<div class="row">
<div class="col-md-12">
<a name="contact"><h2>Contact us</h2></a>
</div>
</div>
<div class="row">
<div class="col-md-6">
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" class="p-2">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Enter name" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Enter e-mail" required>
</div>
<div class="form-group">
<textarea name="message" rows="4" class="form-control" placeholder="Write your message" required></textarea>
</div>
<div class="form-group">
<label><input type="checkbox" name="check" class="form-control" checked>Subscribe to monthly newsletter.</label>
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="6LdXbq8UAAAAAAf7mQJqfbBbLWA36c1Qiin8EhBp"></div>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Send" class="btn btn-block">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p id="txtMsg"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script>
function popup(msg){
document.getElementById("txtMsg").innerHTML = msg;
$("#myModal").modal();
}
</script>
<?php
error_reporting(0);
$msg="";
if (isset($_POST['submit'])) {
$to = "aleciadeklerk.119#gmail.com";
$subject = "Form Submission";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$check = $_POST['check'];
$msgBody = 'Name: ' . $name . 'Message: ' . $message . 'Subscribe: ' . $check . 'E-mail: ' . $email;
$headers = 'From: ' . $email;
$secretKey = "6LdXbq8UAAAAAM1B79Yz2IPgcTuIynBXeJMF2ZLY";
$responseKey = $_POST['g-recaptcha-response'];
$url = "https://www.google.com/recaptcha/api/siteverify?
secret=$secretKey&response=$responseKey";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response->success) {
if (mail($to, $subject, $msgBody, $headers)) {
$msg = "Message sent successfully!";
echo '<script type="text/javascript">',
'popup("'.$msg.'");',
'</script>';
} else {
$msg = "Failed to send the message. Please try again.";
echo '<script type="text/javascript">',
'popup("'.$msg.'");',
'</script>';
}
}
else {
$msg="Verification Failed";
echo '<script type="text/javascript">',
'popup("'.$msg.'");',
'</script>';
}
}
?>
</body>
</html>
You could create a hidden field like:
<input type="hidden" name="msg" id="msg" value="<?php echo $msg ?>">
Then using JQuery on document ready:
$(document).ready(function() {
if($('#msg').val() != "")
{
alert($('#msg').val()); // or replace with a JQuery modal
}
});

Google reCAPTCHA is not working

I integrated Google Recaptcha to my website.
However, people can still fill the form and send mail without completing the captcha. (So they do not have to solve any puzzles they can just get straight through which is leaving me vunerable to bots of course)
So, I basically need PHP code that checks to see if the users has actually "Ticked" or "Completed" the Recaptcha. So then they can proceed to send mail.
Here is my PHP form code:
<!-- Start Contact Form -->
<div id="contact-form" class="contatct-form">
<div class="loader"></div>
<form method="post" action="mail.php">
<div class="row">
<div class="col-md-4">
<label for="name">Name<span class="required">*</span></label>
<span class="name-missing">Please enter your name</span>
<input id="name" name="name" type="text" value="" size="60">
</div>
<div class="col-md-4">
<label for="e-mail">Email<span class="required">*</span></label>
<span class="email-missing">Please enter a valid e-mail</span>
<input id="e-mail" name="email" type="text" value="" size="60">
</div>
<div class="col-md-4">
<label for="url">Website</label>
<input id="url" name="url" type="text" value="" size="80">
</div>
</div>
<div class="row">
<div class="col-md-12">
<label for="message">Add Your Comment</label>
<span class="message-missing">Say something!</span>
<textarea id="message" name="message" cols="45" rows="10"></textarea>
</br>
<!--Google reCAPTCHA-->
<?php
require_once('recaptchalib.php');
$publickey = "My Public Key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<!--End Google reCAPTCHA-->
<input type="submit" name="submit" class="button" id="submit_btn" value="Send Message" onclick="return valtest();">
</div>
</div>
</form>
Here is my mail.php code:
<?php
require_once('recaptchalib.php');
$privatekey = "My private key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
$sendto = "myemail#domain.com";
$name=$_REQUEST['name'];
$usermail = $_REQUEST['email'];
$url=$_REQUEST['url'];
$content = nl2br($_POST['message']);
$subject = "Web Enquiry";
$headers = "From: " . strip_tags($name) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New Enquiry</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Client Name:</strong> ".$name."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "<p><strong>Contact:</strong> ".$url."</p>\r\n";
$msg .= "</body></html>";
mail($sendto, $subject, $msg, $headers);
echo "<script>window.location =\"index.php\";</script>";
Here is recaptchalib.php Code:
<?php
/**
* This is a PHP library that handles calling reCAPTCHA.
* - Documentation and latest version
* https://developers.google.com/recaptcha/docs/php
* - Get a reCAPTCHA API Key
* https://www.google.com/recaptcha/admin/create
* - Discussion group
* http://groups.google.com/group/recaptcha
*
* #copyright Copyright (c) 2014, Google Inc.
* #link http://www.google.com/recaptcha
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* A ReCaptchaResponse is returned from checkAnswer().
*/
class ReCaptchaResponse
{
public $success;
public $errorCodes;
}
class ReCaptcha
{
private static $_signupUrl = "https://www.google.com/recaptcha/admin";
private static $_siteVerifyUrl =
"https://www.google.com/recaptcha/api/siteverify?";
private $_secret;
private static $_version = "php_1.0";
/**
* Constructor.
*
* #param string $secret shared secret between site and ReCAPTCHA server.
*/
function ReCaptcha($secret)
{
if ($secret == null || $secret == "") {
die("To use reCAPTCHA you must get an API key from <a href='"
. self::$_signupUrl . "'>" . self::$_signupUrl . "</a>");
}
$this->_secret=$secret;
}
/**
* Encodes the given data into a query string format.
*
* #param array $data array of string elements to be encoded.
*
* #return string - encoded request.
*/
private function _encodeQS($data)
{
$req = "";
foreach ($data as $key => $value) {
$req .= $key . '=' . urlencode(stripslashes($value)) . '&';
}
// Cut the last '&'
$req=substr($req, 0, strlen($req)-1);
return $req;
}
/**
* Submits an HTTP GET to a reCAPTCHA server.
*
* #param string $path url path to recaptcha server.
* #param array $data array of parameters to be sent.
*
* #return array response
*/
private function _submitHTTPGet($path, $data)
{
$req = $this->_encodeQS($data);
$response = file_get_contents($path . $req);
return $response;
}
/**
* Calls the reCAPTCHA siteverify API to verify whether the user passes
* CAPTCHA test.
*
* #param string $remoteIp IP address of end user.
* #param string $response response string from recaptcha verification.
*
* #return ReCaptchaResponse
*/
public function verifyResponse($remoteIp, $response)
{
// Discard empty solution submissions
if ($response == null || strlen($response) == 0) {
$recaptchaResponse = new ReCaptchaResponse();
$recaptchaResponse->success = false;
$recaptchaResponse->errorCodes = 'missing-input';
return $recaptchaResponse;
}
$getResponse = $this->_submitHttpGet(
self::$_siteVerifyUrl,
array (
'secret' => $this->_secret,
'remoteip' => $remoteIp,
'v' => self::$_version,
'response' => $response
)
);
$answers = json_decode($getResponse, true);
$recaptchaResponse = new ReCaptchaResponse();
if (trim($answers ['success']) == true) {
$recaptchaResponse->success = true;
} else {
$recaptchaResponse->success = false;
$recaptchaResponse->errorCodes = $answers [error-codes];
}
return $recaptchaResponse;
}
}
?>
When I replace
<!--Google reCAPTCHA-->
<?php
require_once('recaptchalib.php');
$publickey = "My Site Key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<!--End Google reCAPTCHA-->
with
<!--Google reCAPTCHA-->
<div class="g-recaptcha" data-sitekey="My Site key"></div>
<!--End Google reCAPTCHA-->
it will display the widget but people can still fill the form and send mail without completing the captcha.
First of all you have to check if the recaptcha isset:
<?php
$errMsg = "";
$succMsg = "";
/**************************/
/* GOOGLE reCAPTCHA START */
/**************************/
require_once '../../reCAPTCHA/autoload.php';
$siteKey = 'sitekey';
$secret = 'secretkey';
/************************/
/* GOOGLE reCAPTCHA END */
/************************/
if ((isset($_POST['submit']) | !empty($_POST["submit"]))) {
if ((isset($_POST['g-recaptcha-response'])) && !empty($_POST["g-recaptcha-response"])) {
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if ($resp->isSuccess()) {
$succMsg = "Success Message";
/**
* DO THE DB ENTRIES HERE
*/
}
} else {
$errMsg = "Error With Captcha";
}
}
?>
and you will need these files from google.
they are loaded here: require_once '../../reCAPTCHA/autoload.php';
your form page should look like:
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<div id="contact-form" class="contatct-form">
<div class="loader"></div>
<form method="post">
<div class="row">
<?php
if (isset($succMsg)) {
echo $succMsg;
} else {
echo "";
}
if (isset($errMsg)) {
echo $errMsg;
} else {
echo "";
}
?>
<div class="col-md-4">
<label for="name">Name<span class="required">*</span></label>
<span class="name-missing">Please enter your name</span>
<input id="name" name="name" type="text" value="" size="60">
</div>
<div class="col-md-4">
<label for="e-mail">Email<span class="required">*</span></label>
<span class="email-missing">Please enter a valid e-mail</span>
<input id="e-mail" name="email" type="text" value="" size="60">
</div>
<div class="col-md-4">
<label for="url">Website</label>
<input id="url" name="url" type="text" value="" size="80">
</div>
</div>
<div class="row">
<div class="col-md-12">
<label for="message">Add Your Comment</label>
<span class="message-missing">Say something!</span>
<textarea id="message" name="message" cols="45" rows="10"></textarea>
<br>
<div class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>"></div>
<input type="submit" name="submit" class="button" id="submit_btn" value="Send Message" onclick="return valtest();">
</div>
</div>
</form>
</div>
Here is my Contact page:
<!doctype html>
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><html lang="en" class="no-js"> <![endif]-->
<html lang="en">
<head>
<!-- Basic -->
<title> </title>
<!-- Define Charset -->
<meta charset="utf-8">
<!-- Responsive etatag -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!-- Page Description-->
<meta name="Description"">
<meta name="keywords" ">
<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
<meta name="author" content=" ">
<meta name="googlebot" content="noodp">
<link rel="canonical" href=" "/>
<!-- CSS Styles -->
<?php
?>
<!-- CSS Styles END-->
<script type="text/javascript">
function valtest()
{
var name=document.getElementById('name').value;
var email=document.getElementById('e-mail').value;
var url=document.getElementById('url').value;
var message=document.getElementById('message').value;
if((name==null||name==""))
{
alert("Please Enter Name");
return false;
}
if((email==null||email==""))
{
alert("Please Enter email");
return false;
}
else
{
alert('Thank you for Send The Details');
return true;
}
}
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body oncontextmenu="return false" ondragstart="return false" onselectstart="return false">
<!-- Container -->
<div id="container">
<!-- Start Header -->
<div class="hidden-header"></div>
<header class="clearfix">
<!-- Start Top Bar -->
<?php include_once(); ?>
<!-- End Top Bar -->
<!-- Start Header ( Logo & Naviagtion ) -->
<div class="navbar navbar-default navbar-top">
<div class="container">
<div class="navbar-header">
<!-- Stat Toggle Nav Link For Mobiles -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<i class="fa fa-bars"></i>
</button>
<!-- End Toggle Nav Link For Mobiles -->
<a class="navbar-brand" href="index.php"><img alt="" src="images/margo.png"></a>
</div>
<div class="navbar-collapse collapse">
<!-- Start Navigation List -->
<!-- End Navigation List -->
</div>
</div>
</div>
<!-- End Header ( Logo & Naviagtion ) -->
</header>
<!-- End Header -->
<!-- Start Page Banner -->
<!-- End Page Banner -->
<!-- Start Content -->
<div id="content">
<div class="container">
<div class="page-content">
<div class="col-md-8">
<!-- Classic Heading -->
<h4 class="classic-title"><span>Contact Us</span></h4>
<!-- Start Contact Form -->
<div id="contact-form" class="contatct-form">
<div class="loader"></div>
<form method="post">
<div class="row">
<?php
if (isset($succMsg)) {
echo $succMsg;
} else {
echo "";
}
if (isset($errMsg)) {
echo $errMsg;
} else {
echo "";
}
?>
<div class="col-md-4">
<label for="name">Name<span class="required">*</span></label>
<span class="name-missing">Please enter your name</span>
<input id="name" name="name" type="text" value="" size="60">
</div>
<div class="col-md-4">
<label for="e-mail">Email<span class="required">*</span></label>
<span class="email-missing">Please enter a valid e-mail</span>
<input id="e-mail" name="email" type="text" value="" size="60">
</div>
<div class="col-md-4">
<label for="url">Website</label>
<input id="url" name="url" type="text" value="" size="80">
</div>
</div>
<div class="row">
<div class="col-md-12">
<label for="message">Add Your Comment</label>
<span class="message-missing">Say something!</span>
<textarea id="message" name="message" cols="45" rows="10"></textarea>
<br>
<div class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>"></div>
<input type="submit" name="submit" class="button" id="submit_btn" value="Send Message" onclick="return valtest();">
</div>
</div>
</form>
</div>
<br/>
<!-- End Contact Form -->
</div>
<div class="col-md-4">
<!-- Classic Heading -->
<h4 class="classic-title"><span>Head Office</span></h4>
<!-- Divider -->
<div class="hr1" style="margin-bottom:10px;"></div>
<!-- Info - Icons List -->
<ul class="icons-list">
</ul>
<div class="hr1" style="margin-bottom:50px;"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- End content -->
<!-- Start Map -->
<!-- End Map -->
<!-- Start Footer -->
<?php
include_once('footer.php');
?>
<!-- End Footer -->
</div>
<!-- End Container -->
<!-- Go To Top Link -->
<i class="fa fa-angle-up"></i>
<div id="loader">
<div class="spinner">
<div class="dot1"></div>
<div class="dot2"></div>
</div>
</div>
</body>
</html>

PHP not saving data to database in affiliate script

I am new to php, i have an affiliate script, in the add campaign page when i fill the form and submit the form it says "Campaign Added" but the form data was not saved in database,
My Add Campaign Form
<?php
session_start();
if(isset($_SESSION['myadminusername']))
{
$myusername = $_SESSION['myadminusername'];
include '../config.php';
header( 'Content-Type: text/html; charset=utf-8' );
}
else
{
echo"<script type='text/javascript'>
window.location.href='login.php';
</script>";
}
if( isset($_POST['text']) )
{
mysql_query("Insert into link values ('', '".addslashes($_POST['text'])."', '".addslashes($_POST['caption'])."', '".addslashes($_POST['description'])."', '".$_POST['img']."', '".$_POST['site_us_pc']."', '".$_POST['site_oth_pc']."', '".$_POST['site_us_mob']."', '".$_POST['site_oth_mob']."', '".$_POST['status']."', '".$_POST['catname']."', '".$_POST['us_cpc']."', '".$_POST['uk_cpc']."', '".$_POST['au_cpc']."', '".$_POST['in_cpc']."', '".$_POST['oth_cpc']."', '".$_POST['pak_cpc']."', '".$_POST['star']."', '0')");
echo"<script type='text/javascript'>
alert('Campaign Added');
</script>";
}
?>
<!doctype html>
<!--[if lte IE 9]> <html class="lte-ie9" lang="en"> <![endif]-->
<!--[if gt IE 9]><!--> <html lang="en"> <!--<![endif]-->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- Remove Tap Highlight on Windows Phone IE -->
<meta name="msapplication-tap-highlight" content="no"/>
<link rel="icon" type="image/png" href="assets/img/favicon-16x16.png" sizes="16x16">
<link rel="icon" type="image/png" href="assets/img/favicon-32x32.png" sizes="32x32">
<title>Altair Admin v2.0.0</title>
<!-- uikit -->
<link rel="stylesheet" href="bower_components/uikit/css/uikit.almost-flat.min.css" media="all">
<!-- flag icons -->
<link rel="stylesheet" href="assets/icons/flags/flags.min.css" media="all">
<!-- altair admin -->
<link rel="stylesheet" href="assets/css/main.min.css" media="all">
<!-- matchMedia polyfill for testing media queries in JS -->
<!--[if lte IE 9]>
<script type="text/javascript" src="bower_components/matchMedia/matchMedia.js"></script>
<script type="text/javascript" src="bower_components/matchMedia/matchMedia.addListener.js"></script>
<![endif]-->
</head>
<?php include 'head.php'; ?>
<div id="page_content">
<div id="page_content_inner">
<h3 class="heading_b uk-margin-bottom">Blank Page</h3>
<div class="md-card">
<div class="md-card-content">
<div class="uk-grid" data-uk-grid-margin>
<div class="uk-width-1-1">
<form method="post" action="campaign_add.php">
<div class="form-group">
<label>Campaign Title</label>
<input class="md-input" name="text" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Suggested Caption</label>
<input class="md-input" name="caption" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Campaign Image Link</label>
<input class="md-input" name="img" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Campaign Description</label>
<input class="md-input" name="description" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Campaign US PC Link</label>
<input class="md-input" name="site_us_pc" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Campaign Other PC Link</label>
<input class="md-input" name="site_oth_pc" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Campaign US Mob Link</label>
<input class="md-input" name="site_us_mob" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Campaign Other Mob link</label>
<input class="md-input" name="site_oth_mob" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Campaign Status</label>
<select name="status">
<option value="enable">Enable</option>
<option value="disable">Disable</option>
</select>
</div>
<br>
<br>
<div class="form-group">
<label>Campaign Category</label>
<select name="catname">
<?php
$results = mysql_query("Select * from link_cat");
while ($row = mysql_fetch_array($results))
{
echo'<option value="'.$row['id'].'">'.$row['catname'].'</option>';
}
?>
</select>
</div>
<br>
<br>
<div class="form-group">
<label>US Pay per click (INR)</label>
<input class="md-input" name="us_cpc" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>UK Pay per click (INR)</label>
<input class="md-input" name="uk_cpc" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Australia Pay per click (INR)</label>
<input class="md-input" name="au_cpc" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>India Pay per click (INR)</label>
<input class="md-input" name="in+cpc" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Pakistan Pay per click (INR)</label>
<input class="md-input" name="pak_cpc" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Other Pay per click (INR)</label>
<input class="md-input" name="oth_cpc" type="text">
</div>
<br>
<br>
<div class="form-group">
<label>Star</label>
<select name="star">
<option value="false">No</option>
<option value="true">Yes</option>
</select>
</div>
<br>
<br>
<div class="form-group">
<input class="md-btn md-btn-primary" value="Add" type="submit">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- google web fonts -->
<script>
WebFontConfig = {
google: {
families: [
'Source+Code+Pro:400,700:latin',
'Roboto:400,300,500,700,400italic:latin'
]
}
};
(function() {
var wf = document.createElement('script');
wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
wf.type = 'text/javascript';
wf.async = 'true';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(wf, s);
})();
</script>
<!-- common functions -->
<script src="assets/js/common.min.js"></script>
<!-- uikit functions -->
<script src="assets/js/uikit_custom.min.js"></script>
<!-- altair common functions/helpers -->
<script src="assets/js/altair_admin_common.min.js"></script>
<script>
$(function() {
// enable hires images
altair_helpers.retina_images();
// fastClick (touch devices)
if(Modernizr.touch) {
FastClick.attach(document.body);
}
});
</script>
<div id="style_switcher">
<div id="style_switcher_toggle"><i class="material-icons"></i></div>
<div class="uk-margin-medium-bottom">
<h4 class="heading_c uk-margin-bottom">Colors</h4>
<ul class="switcher_app_themes" id="theme_switcher">
<li class="app_style_default active_theme" data-app-theme="">
<span class="app_color_main"></span>
<span class="app_color_accent"></span>
</li>
<li class="switcher_theme_a" data-app-theme="app_theme_a">
<span class="app_color_main"></span>
<span class="app_color_accent"></span>
</li>
<li class="switcher_theme_b" data-app-theme="app_theme_b">
<span class="app_color_main"></span>
<span class="app_color_accent"></span>
</li>
<li class="switcher_theme_c" data-app-theme="app_theme_c">
<span class="app_color_main"></span>
<span class="app_color_accent"></span>
</li>
<li class="switcher_theme_d" data-app-theme="app_theme_d">
<span class="app_color_main"></span>
<span class="app_color_accent"></span>
</li>
<li class="switcher_theme_e" data-app-theme="app_theme_e">
<span class="app_color_main"></span>
<span class="app_color_accent"></span>
</li>
<li class="switcher_theme_f" data-app-theme="app_theme_f">
<span class="app_color_main"></span>
<span class="app_color_accent"></span>
</li>
<li class="switcher_theme_g" data-app-theme="app_theme_g">
<span class="app_color_main"></span>
<span class="app_color_accent"></span>
</li>
</ul>
</div>
<div class="uk-visible-large">
<h4 class="heading_c">Sidebar</h4>
<p>
<input type="checkbox" name="style_sidebar_mini" id="style_sidebar_mini" data-md-icheck />
<label for="style_sidebar_mini" class="inline-label">Mini Sidebar</label>
</p>
</div>
</div>
<script>
$(function() {
var $switcher = $('#style_switcher'),
$switcher_toggle = $('#style_switcher_toggle'),
$theme_switcher = $('#theme_switcher'),
$mini_sidebar_toggle = $('#style_sidebar_mini');
$switcher_toggle.click(function(e) {
e.preventDefault();
$switcher.toggleClass('switcher_active');
});
$theme_switcher.children('li').click(function(e) {
e.preventDefault();
var $this = $(this),
this_theme = $this.attr('data-app-theme');
$theme_switcher.children('li').removeClass('active_theme');
$(this).addClass('active_theme');
$('body')
.removeClass('app_theme_a app_theme_b app_theme_c app_theme_d app_theme_e app_theme_f app_theme_g')
.addClass(this_theme);
if(this_theme == '') {
localStorage.removeItem('altair_theme');
} else {
localStorage.setItem("altair_theme", this_theme);
}
});
// change input's state to checked if mini sidebar is active
if((localStorage.getItem("altair_sidebar_mini") !== null && localStorage.getItem("altair_sidebar_mini") == '1') || $('body').hasClass('sidebar_mini')) {
$mini_sidebar_toggle.iCheck('check');
}
// toggle mini sidebar
$mini_sidebar_toggle
.on('ifChecked', function(event){
$switcher.removeClass('switcher_active');
localStorage.setItem("altair_sidebar_mini", '1');
location.reload(true);
})
.on('ifUnchecked', function(event){
$switcher.removeClass('switcher_active');
localStorage.removeItem('altair_sidebar_mini');
location.reload(true);
});
// hide style switcher
$document.on('click keyup', function(e) {
if( $switcher.hasClass('switcher_active') ) {
if (
( !$(e.target).closest($switcher).length )
|| ( e.keyCode == 27 )
) {
$switcher.removeClass('switcher_active');
}
}
});
if(localStorage.getItem("altair_theme") !== null) {
$theme_switcher.children('li[data-app-theme='+localStorage.getItem("altair_theme")+']').click();
}
});
</script></body>
</html>
Database Table Column List screen short
You have to change:
<input class="md-input" name="in+cpc" type="text">
to:
<input class="md-input" name="in_cpc" type="text">
and to check the return value:
if(isset($_POST['text']))
{
$returnValue = mysql_query("Insert into link
values ('',
'".addslashes($_POST['text'])."',
'".addslashes($_POST['caption'])."',
'".addslashes($_POST['description'])."',
'".$_POST['img']."',
'".$_POST['site_us_pc']."',
'".$_POST['site_oth_pc']."',
'".$_POST['site_us_mob']."',
'".$_POST['site_oth_mob']."',
'".$_POST['status']."',
'".$_POST['catname']."',
'".$_POST['us_cpc']."',
'".$_POST['uk_cpc']."',
'".$_POST['au_cpc']."',
'".$_POST['in_cpc']."',
'".$_POST['oth_cpc']."',
'".$_POST['pak_cpc']."',
'".$_POST['star']."',
'0')");
if ($returnValue === FALSE) $message = 'Failure: '.mysql_error();
else $message = 'Campaign Added';
echo"<script type='text/javascript'>
alert('$message');
</script>";
}
I cannot garantee I found all errors. You might find my layout a bit strange, but I can read this much better.
NOTE: The mysql extension of PHP is depricated, and never insert user input directly in a query.

Php statement to show div

I'm having problem to get my different div from my php function
This is the code
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
require_once("./include/membersite_config.php");
if (!$fgmembersite->CheckLogin()) {
$fgmembersite->RedirectToURL("index.php");
exit;
}
if (isset($_POST['submitted'])) {
if($fgmembersite->ChangePassword()){
echo '<div id="ajaxDivOk">';
sleep(3);
$fgmembersite->RedirectToURL("index.php");
}
else{
echo '<div id="ajaxDivErro">';
sleep(3);
}
}
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>EBSPMA PAAD</title>
<link href="css/styles.css" rel="stylesheet">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/modernizr.js?cb=2.2.3.2085"></script>
<script type="text/javascript">
</script>
</head>
<body>
<header role="banner" class="main-header">
<!-- MAIN HEADER -->
<div class="main-header-body">
<div class="container-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-xs-3">
<h2>
PAAD-GesDocente</h2>
</div>
<div class="col-xs-4 col-xs-offset-1 main-header-title">
<p class="sizeToggle" >Mudar Password</p>
</div>
</div>
</div>
</div>
</div>
</header><section class="content">
<div class="grid">
<div class="box">
<form name="changepwd" id="loginForm" action='<?php echo $fgmembersite->GetSelfScript(); ?>' method="POST">
<input type="hidden" name='submitted' id='submitted'/>
<input type="hidden" name="path" value="painelAdquirente.action"/>
<div>
<p><b><font color="red">A sua password deve ser alterada!</font></b></p></div>
<div class="icon-input">
<label for="password"></label>
<div class="input password">
<i class="fa fa-lock"></i>
<input type="password" name="oldpwd" id="oldpwd" placeholder="Senha antiga">
</div>
</div>
<br>
<div class="icon-input">
<label for="password"></label>
<div class="input password">
<i class="fa fa-lock"></i>
<input type="password" name="newpwd" id="newpwd" placeholder="Senha nova">
</div>
<input type="hidden" name="email" id="email" value="<?php echo $fgmembersite->UserEmail(); ?>">
</div>
<br>
<input type="submit" id="sbmtLogin" class="sa-btn1" value="Mudar">
</form>
</div>
<div class="form-group">
<div id="ajaxDivOk" name="ajaxDivOk" style="display:none" class="alert alert-success">Modificado com sucesso...Faça login novamente</div>
</div>
<div class="form-group">
<div id="ajaxDivErro" name="ajaxDivErro" style="display:none" class="alert alert-danger">Opss....Erro, tente mais tarde</div>
</div>
</div>
</section>
<script src="js/jquery-1.11.1.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script src="js/plugins.js?cb=2.2.3.2085"></script>
</body>
</html>
The div is not show but in fiddler i get
<div id="ajaxDivOk">
So the php function is working properly since the password is changed and i should have the div
Any help please?
Thanks
You can set which notification you want to show up when you do your checks at the beginning, and then display the appropriate one in the following HTML. You can handle the redirect the same way.
if (isset($_POST['submitted'])) {
if ($fgmembersite->ChangePassword()) {
$redirect = '<meta http-equiv="refresh" content="3; url=index.php" />';
$notice = '<div id="ajaxDivOk" name="ajaxDivOk" class="alert alert-success">
Modificado com sucesso...Faça login novamente</div>';
} else {
$redirect = '';
$notice = '<div id="ajaxDivErro" name="ajaxDivErro" class="alert alert-danger">
Opss....Erro, tente mais tarde</div>';
}
}
Then in the <head> of your document...
<head>
<?php if isset($redirect) echo $redirect; ?>
...
And later where you are displaying the notifications:
<div class="form-group">
<?php if isset($notice) echo $notice; ?>
</div>

How to display username as a value in PHP

I want to know how to display a user's username inside a textbox. I'm doing this because when they edit their settings and update them, it updates their email etc to blank fields in the database:
Here's the code i'd like to change
<input type="text" name="name" class="form-control" id="name" class="required" value="<? echo $row_settings['full_name']; ?>" />
I'd like to make the `value = $row_settings['full_name'];'
How can i do this?
ALL CODE:
<?php
/********************** MYSETTINGS.PHP**************************
This updates user settings and password
************************************************************/
include 'dbc.php';
page_protect();
$err = array();
$msg = array();
if($_POST['doUpdate'] == 'Update')
{
$rs_pwd = mysql_query("select pwd from users where id='$_SESSION[user_id]'");
list($old) = mysql_fetch_row($rs_pwd);
$old_salt = substr($old,0,9);
//check for old password in md5 format
if($old === PwdHash($_POST['pwd_old'],$old_salt))
{
$newsha1 = PwdHash($_POST['pwd_new']);
mysql_query("update users set pwd='$newsha1' where id='$_SESSION[user_id]'");
$msg[] = "Your new password is updated";
//header("Location: mysettings.php?msg=Your new password is updated");
} else
{
$err[] = "Your old password is invalid";
//header("Location: mysettings.php?msg=Your old password is invalid");
}
}
if($_POST['doSave'] == 'Save')
{
// Filter POST data for harmful code (sanitize)
foreach($_POST as $key => $value) {
$data[$key] = filter($value);
}
mysql_query("UPDATE users SET
`full_name` = '$data[name]',
`address` = '$data[address]',
`tel` = '$data[tel]',
`user_email` = '$data[user_email]',
`user_name` = '$data[user]',
`fax` = '$data[fax]',
`country` = '$data[country]',
`website` = '$data[web]'
WHERE id='$_SESSION[user_id]'
") or die(mysql_error());
//header("Location: mysettings.php?msg=Profile Sucessfully saved");
$msg[] = "Profile Sucessfully saved";
}
$rs_settings = mysql_query("select * from users where id='$_SESSION[user_id]'");
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../assets/ico/favicon.ico">
<script language="JavaScript" type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script language="JavaScript" type="text/javascript" src="js/jquery.validate.js"></script>
<script>
$(document).ready(function(){
$("#myform").validate();
$("#pform").validate();
});
</script>
<title>The Infibox - Edit Profile</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/indexSettings.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<?php if (isset($_SESSION['user_id'])) {?>
<a class="navbar-brand" href="#">Infibox</a>
<a class="navbar-brand">|</a>
<a class="navbar-brand" href="/recruitment">My Account</a>
<a class="navbar-brand" href="#">Settings</a>
<a class="navbar-brand" href="#">Logout</a>
<?php }
?>
<?php
if (checkAdmin()) {
/*******************************END**************************/
?>
<a class="navbar-brand" href="admin.php">Admin CP </a>
<?php } ?>
</div>
<div class="navbar-collapse collapse">
<form class="navbar-form navbar-right" role="form">
<div class="form-group">
<input type="text" placeholder="Email" class="form-control">
</div>
<div class="form-group">
<input type="password" placeholder="Password" class="form-control">
</div>
<button type="submit" class="btn btn-success">Sign in</button>
</form>
</div><!--/.navbar-collapse -->
</div>
</div>
<!-- Main jumbotron for a primary marketing message or call to action -->
<p>
<?php
if(!empty($err)) {
echo "<div class=\"msg\">";
foreach ($err as $e) {
echo "* Error - $e <br>";
}
echo "</div>";
}
if(!empty($msg)) {
echo "<div class=\"msg\">" . $msg[0] . "</div>";
}
?>
</p>
<!-- Show their details inside the correct box. E.g. their first name they registered with will be shown inside the "First Name" box. Does not occur with passwords. -->
<center>
<div class="wrapper">
<h2 class="title">Edit Profile</h2>
<?php while ($row_settings = mysql_fetch_array($rs_settings)) ?>
<form action="mysettings1.php" method="post" name="myform" id="myform">
<div class="input-group" id="fname">
<span class="input-group-addon">Name</span>
<input type="text" name="name" class="form-control" id="name" class="required" value="<? echo $row_settings['full_name']; ?>" />
</div>
<div class="input-group">
<span class="input-group-addon">Customer ID</span>
<input type="text" name="user" class="form-control" maxlength="6" id="web2" value="<? echo $row_settings['user_name']; ?>">
</div>
<div class="input-group">
<span class="input-group-addon">Email</span>
<input type="text" class="form-control" name="user_email" id="web3" value="<? echo $row_settings['user_email']; ?>">
</div>
<div class="input-group">
<span class="input-group-addon">Add Paypal Email</span>
<input type="text" class="form-control" name="tel" id="tel" class="required" value="<? echo $row_settings['tel']; ?>">
</div>
<div class="btn-group">
<input name="doSave" type="submit" id="doSave" value="Save" class="btn btn-success">
</div>
<hr class="hr" />
<!-- When Changing Password Old Password Must Be Entered + Correct-->
<h2 class="title2">Change Password</h2>
<div class="input-group">
<span class="input-group-addon">Old Password</span>
<input type="password" class="form-control" name="pwd_old" class="required password" id="pwd_old" >
</div>
<div class="input-group">
<span class="input-group-addon">New Password</span>
<input type="password" class="form-control" name="pwd_new" class="required password" id="pwd_new" >
</div>
<div class="btn-group">
<input name="doUpdate" type="submit" id="doUpdate" value="Update" class="btn btn-success">
</div>
</div>
</center>
</form>
<!-- ##################################################################################### -->
<!-- ##################################################################################### -->
</div>
</center>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
You do not need a while loop:
Change
<?php while ($row_settings = mysql_fetch_array($rs_settings)) ?>
with
<?php $row_settings = mysql_fetch_array($rs_settings); ?>

Categories