Reputation: 35953
I have downloaded this "subscribe to our newsletter" script from here.
The script contains a form and a mySQL database with 6 fields: id
, email
, name
, date_subscribe
, date_unsubscribe
and status
.
The form is very simple: type your email, type your name and subscribe.
I need to do a modification to this form/database to add an extra field I call promotion
. I have edited the mySQL database and added promotion
as varchar(250) after status
.
I have modified the PHP script to include this new field but when I submit the form, the content on the new promotion
field is not added to the database.
I have checked the code and form several times and I don't see the problem.
This is the form HTML after the modification (my modifications are marked):
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Subscription form</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800' rel='stylesheet' type='text/css'>
<link href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<script src="js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- Add your site or application content here -->
<form id="subscribe-form" class="subscribe-form hide">
<div class="form-title">Subscribe to our newsletter</div>
<hr>
<div class="form-loading hide"><i class="fa fa-circle-o-notch fa-spin"></i></div>
<div class="form-message hide">
Thank you for subscribing!</div>
<div class="form-content">
<div class="group">
<label for="email">Your email</label>
<div class="input-prefix">
<i class="fa fa-user"></i>
<input id="email" name="email" class="form-control" type="email" placeholder="Your email">
</div>
</div>
<div class="group">
<label for="name">Your name</label>
<div class="input-prefix">
<i class="fa fa-envelope"></i>
<input id="name" name="name" class="form-control" type="text" placeholder="Your name">
</div>
</div>
I HAVE ADDED THIS SECTION
===========================
<div class="group">
<label for="promotion">PROMOTION NAME (Fill this with the promotion name)</label>
<div class="input-prefix">
<input id="promotion" name="promotion" class="form-control" type="text" placeholder="Promotion Name">
</div>
</div>
============================
<div class="group group-submit">
<label class="empty"></label>
<div align="right"><button class="btn-submit" type="submit">Subscribe</button></div>
</div>
</div>
<!--<div class="form-footer">
<a class="btn" data-toggle="unsubscribe" href="#">Unsubscribe</a>
</div>-->
<input type="hidden" name="action" value="subscribe">
</form>
<form id="unsubscribe-form" class="subscribe-form hide">
<div class="form-title unsubscribe">Unsubscribe from our newsletter</div>
<hr>
<div class="form-loading hide"><i class="fa fa-circle-o-notch fa-spin"></i></div>
<div class="form-message hide">
You've been unsubscribed.</div>
<div class="form-content">
<div class="group">
<label for="unsubscribe_email">Your email</label>
<div class="input-prefix">
<i class="fa fa-user"></i>
<input id="unsubscribe_email" name="unsubscribe_email" class="form-control" type="email" placeholder="Your email">
</div>
</div>
<div class="group">
<label class="empty"></label>
<div>
<input id="confirm" type="checkbox" name="confirm" value="T">
<label for="confirm">Click to confirm you want to unsubscribe</label>
</div>
</div>
<div class="group group-submit">
<label class="empty"></label>
<div align="right"><button class="btn-submit" type="submit">Unsubscribe</button></div>
</div>
</div>
<div class="form-footer">
<a class="btn" data-toggle="subscribe" href="#">Subscribe</a>
</div>
<input type="hidden" name="action" value="unsubscribe">
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
</body>
</html>
This is the PHP that populates the database and my modifications:
<?php
###########################################################
/*
Subscription Form
Copyright (C) StivaSoft ltd. All rights Reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.html.
For further information visit:
http://www.phpjabbers.com/
[email protected]
*/
###########################################################
# CONFIG
define('_DB_HOST', 'localhost');
define('_DB_NAME', 'news');
define('_DB_USER', 'admin_xxxx');
define('_DB_PASS', 'xxx');
$subscribe = (isset($_POST['action']) && $_POST['action'] == 'unsubscribe')?false:true;
if ($subscribe){
$fields = array(
array('name' => 'email', 'valid' => array('require', 'email')),
array('name' => 'name', 'valid' => array('require')),
);
}else{
$fields = array(
array('name' => 'unsubscribe_email', 'valid' => array('require', 'email')),
array('name' => 'confirm', 'valid' => array('require'), 'err_message' => 'Please confirm'),
);
}
// Connect to database
$connection = mysql_connect(_DB_HOST, _DB_USER, _DB_PASS) or die ('Unable to connect to MySQL server.<br ><br >Please make sure your MySQL login details are correct.');
$db = mysql_select_db(_DB_NAME, $connection) or die ('request "Unable to select database."');
$error_fields = array();
$get = array();
foreach ($fields AS $field){
$value = isset($_POST[$field['name']])?$_POST[$field['name']]:'';
if (is_array($value)){
$value = implode('/ ', $value);
}
if (get_magic_quotes_gpc()){
$value = stripslashes($value);
}
$get[$field['name']] = mysql_real_escape_string($value);
$is_valid = true;
$err_message = '';
if (!empty($field['valid'])){
foreach ($field['valid'] AS $valid) {
switch ($valid) {
case 'require':
$is_valid = $is_valid && strlen($value) > 0;
$err_message = 'Field required';
break;
case 'email':
$is_valid = $is_valid && preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $value);
$err_message = 'Email required';
break;
default:
break;
}
}
}
if (!$is_valid){
if (!empty($field['err_message'])){
$err_message = $field['err_message'];
}
$error_fields[] = array('name' => $field['name'], 'message' => $err_message);
}
}
if (empty($error_fields)){
if ($subscribe){
$data = array(
'email' => "'".$get['email']."'",
'name' => "'".$get['name']."'",
'date_subscribe' => 'NOW()',
'status' => "'T'",
I HAVE ADDED THIS
============================================
'promotion' => "'".$get['promotion']."'"
============================================
);
$sql = "REPLACE INTO subscription_form (`".implode("`, `", array_keys($data))."`) VALUES(".implode(", ", array_values($data)).")";
}else{
$sql = "UPDATE subscription_form SET date_unsubscribe = NOW(), status = 'F' WHERE email = '".$get['unsubscribe_email']."'";
}
if (!empty($sql)){
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
}
echo (json_encode(array('code' => 'success')));
}else{
echo json_encode(array('code' => 'failed', 'fields' => $error_fields));
}
what am I missing?
Upvotes: 1
Views: 148
Reputation: 12795
Also add the promotion
field here:
if ($subscribe){
$fields = array(
array('name' => 'email', 'valid' => array('require', 'email')),
array('name' => 'name', 'valid' => array('require')),
);
Upvotes: 0
Reputation: 1501
Something like this in your php?
if ($subscribe){
$fields = array(
array('name' => 'email', 'valid' => array('require', 'email')),
array('name' => 'name', 'valid' => array('require')),
array('name' => 'promotion', 'valid' => array()),
);
}
Upvotes: 1
Reputation: 753
In your php try to change this
'promocao' => "'".$get['promocao']."'"
to this
'promocao' => "'".$get['promotion']."'"
Since your input field is name="promotion"
And also be aware that in DB field name is promocao
If not and if in your DB your field name is promotion then change it like this
'promotion' => "'".$get['promotion']."'"
Upvotes: 0