Well, I just bought a sms api which is created by php. They provide me this 2 lines:
http://fahimit.com/smsapi.php?
user=username&pass=pass&phone=mobile_numer&senderid=sender_name&message=my_message
\well now i'm using a html form to send sms. But after message send it's not showing full sender name and message.
For example if i use "Test Sender" as sender_name and "Test message test message" as my_message then it's only showing First word like: Test as sender_name and Test as my_message. I don't understand why it's not showing full message and sender name
PHP code:
<?php
if(isset($_POST['Submit']) && $_POST['Submit'] == "Send SMS")
{
$write_numer = $_POST['write_number'];
$sender = inputvalid($_POST['sender']);
$type = inputvalid($_POST['type']);
$select_msg = inputvalid($_POST['select_msg']);
$msg = $_POST['txt'];
$length = strlen($msg);
$err = array();
$ip = $_SERVER['REMOTE_ADDR'];
if(isset($write_numer) && isset($sender) && isset($type) && isset($select_msg) && isset($msg))
{
if(empty($write_numer) && empty($sender) && empty($type) && empty($select_msg) && empty($msg))
$err[] = "All field require";
else
{
if(empty($write_numer))
$err[] = "Write your mobile number";
elseif(strlen($write_numer) > 13 || strlen($write_numer) < 13)
$err[] = "Your mobile number format is not correct";
elseif(!is_numeric($write_numer))
$err[] = "Your mobile number format is not correct";
elseif (!preg_match("/^8801(6|5|7|8|9|1)\d{8}/", $write_numer))
$err[] = "Invalid mobile number";
if(empty($sender))
$err[] = "Select sender name";
if(empty($type))
$err[] = "Select your message type";
if(empty($select_msg) && empty($msg))
$err[] = "Select your message";
}
}
//error count
if(!empty($err))
{
echo "<div class='error'>";
foreach($err as $er)
{
echo "<font color=red>$er.</font><br/>";
}
echo "</div>";
echo "<br/>";
}
else
{
$sms = "http://fahimit.com/smsapi.php?user=MYUSERNAME&pass=MYPASS&phone=".$write_numer."&senderid=".$sender."&message=".$msg."";
$sms = file_get_contents($sms);
if($sms)
{
echo "<div class='success'>Successfully sent your message to $write_numer. Thank You.</div>";
$sql = mysql_query("INSERT INTO e_sent_sms VALUES('', '', '', '$write_numer', '$msg', '', '', '$length', '$type', '$sender', '$current_date', '$ip' )");
}
else
{
mysql_error();
}
}
}
?>
HTML CODE:
<form name="frm" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>">
<table width="800" border="0" cellspacing="10" cellpadding="0" style="float:left; position:relative;">
<tr>
<td>Write Number</td>
<td><input type="text" name="write_number" placeholder="Write your mobile number" class="td3" value="<?php if(isset($_POST['write_number'])) echo $_POST['write_number']; ?>" /><span style="color:#033;">* Mobile number must start with 8801XXXXXXXXX</span></td>
</tr>
<tr>
<td>Sender</td>
<td><input type="text" name="sender" placeholder="Sender name" class="td3" value="<?php if(isset($_POST['sender'])) echo $_POST['sender']; ?>" /></td>
</tr>
<tr>
<td>Message type</td>
<td>
<select name="type" class="select">
<option value="">--Select--</option>
<option value="5" <?php if(isset($_POST['type']) && $_POST['type'] == "5") echo 'selected = "selected"'; ?>>Text</option>
<option value="1" <?php if(isset($_POST['type']) && $_POST['type'] == "1") echo 'selected = "selected"'; ?>>Flash</option>
<option value="3" <?php if(isset($_POST['type']) && $_POST['type'] == "3") echo 'selected = "selected"'; ?>>Arabic</option>
<option value="2" <?php if(isset($_POST['type']) && $_POST['type'] == "2") echo 'selected = "selected"'; ?>>Unicode</option>
<option value="6" <?php if(isset($_POST['type']) && $_POST['type'] == "6") echo 'selected = "selected"'; ?>>Unicode Flash</option>
<option value="4" <?php if(isset($_POST['type']) && $_POST['type'] == "4") echo 'selected = "selected"'; ?>>Wap Push</option>
</select>
</td>
</tr>
<tr>
<td>Select message</td>
<td>
<select name="select_msg" class="select" id="carDealer">
<option value="">--Select Message--</option>
<?php
$sql = mysql_query("SELECT DISTINCT msg FROM e_sms_draft");
while($res = mysql_fetch_array($sql))
{
$draft = inputvalid($res['msg']);
$draft = stripslashes($draft);
if(isset($_POST['select_msg']) && $_POST['select_msg'] == "$draft")
$sel = 'selected = "selecteds"';
else
$sel = "";
echo "<option value='$draft' $sel>$draft</option>";
}
?>
</select>
</td>
</tr>
<td valign="top">Message</td>
<td><textarea class="textarea2" id="carPark" placeholder="Your message" name="txt" onkeyup="counter(this);"><?php if(isset($_POST['txt'])) echo $_POST['txt']; ?></textarea>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function putIt(e) {
$("#carPark").val(e.target.value);
}
$("#carDealer").on("change", putIt);
</script>
<br/><input type="" name="lbl" style="border:none;"><br/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Save SMS" class="view"/>
<input type="submit" name="Submit" value="Send SMS" class="submit"/></td>
</tr>
</table>
</form>
Can you tell me what's wrong in my code ?
You need to urlencode your text before passing it to the URL. Do like this
$sender = urlencode($sender);
$msg = urlencode($msg);
$sms = "http://fahimit.com/smsapi.php?user=MYUSERNAME&pass=MYPASS&phone=".$write_numer."&senderid=".$sender."&message=".$msg."";
Do this for all the variables that you are going to send it to the URL.
Related
I actually developping an intranet solution for my work,
The thing is that i want to update and operation and add the same result into one of my 3 different tables depending on the -operation- option,
in my initial code if the modification is ok the data will be updated into "taches_redaction" table,
in revenche i need to add 3 different rules,
soif operation is equal to 1 up date taches_redction and add the data into copie table,
soif operation is equal to 2 up date taches_redction and add the data into colla table,
soif operation is equal to 3 up date taches_redction and add the data into archive table,
all of this to have a precise historical record on echa changes in my operations and have some values to get statistics for each team member on the company...
<?php
include '../theme/header-global.php';
?>
<?php
/*
Allows user to edit specific entry in database
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($serie, $user_id, $journee, $titre, $region, $role, $operation, $date, $error)
{
?>
<div style="width:100%">
<center><h1>تحيين التوزيع</h1></center>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="serie" value="<?php echo $serie; ?>" readonly="readonly" />
<table cellpadding="3" cellpadding="3" align="center" id=MyTable width="650">
<tr>
<td align="center">الإسم</td>
<td align="center">
<?php
// get results from database
$usage = mysql_query("SELECT t.* , o.* FROM users t,taches_redaction o WHERE t.id=o.user_id GROUP BY user_name")
or die(mysql_error());
while($row = mysql_fetch_assoc( $usage )) {
if($row['user_id'] == $user_id)
{ // echo out the contents of each row into a table
echo '' . $row['user_name'] . '';
} else {
// do something else
echo '';
}
}
?>
</td>
<td>
<select id="user_id" name="user_id">
<option value="0"></option>
<?php
$sql = mysql_query('SELECT * FROM users');
while($row = mysql_fetch_array($sql)){
if($row['id'] == $user_id)
{
echo '<option value=' . $row['id'] . ' selected=selected>' . $row['user_name'] . '</option>';
} else {
// do something else
echo '<option value=' . $row['id'] . '>' . $row['user_name'] . '</option>';
}
}
?>
</select>
</td>
</tr>
<tr>
<td align="center">اليومية</td>
<td align="center"><input type="text" name="journee" value="<?php echo $journee; ?>" /></td>
<td></td>
</tr>
<tr>
<td align="center">الرسم العقاري</td>
<td align="center">
<input type="text" name="titre" value="<?php echo $titre; ?>" />
<?php if (!empty($region) && $region == '4' ) echo 'FR'; ?>
<?php if (!empty($region) && $region == '1') echo 'بن عروس'; ?>
<?php if (!empty($region) && $region == '2') echo 'زغوان'; ?>
<?php if (!empty($region) && $region == '3') echo 'تونس'; ?>
</td>
<td>
<select name="region">
<option value="4" <?php if (!empty($region) && $region == '4' ) echo 'selected = "selected"'; ?>>FR</option>
<option value="1" <?php if (!empty($region) && $region == '1') echo 'selected = "selected"'; ?>>بن عروس</option>
<option value="2" <?php if (!empty($region) && $region == '2') echo 'selected = "selected"'; ?>>زغوان</option>
<option value="3" <?php if (!empty($region) && $region == '3') echo 'selected = "selected"'; ?>>تونس</option>
</select>
</td>
</tr>
<tr>
<td align="center">المهام</td>
<td align="center">
<select id="role" name="role">
<option value="0"></option>
<option></option>
</select>
</td>
<td>
</td>
</tr>
<tr>
<td align="center">العملية</td>
<td align="center">
<?php
$fonki = mysql_query("SELECT * FROM fonctions")
or die(mysql_error());
// get results from database
$operage = mysql_query("SELECT t.* , o.* FROM taches_redaction t,operations o WHERE t.operation=o.oper_id GROUP BY operation")
or die(mysql_error());
// display data in table
/* echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>"; */
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_assoc( $operage )) {
if($row['oper_id'] == $operation)
{ // echo out the contents of each row into a table
echo '' . $row['operation_name'] . '';
} else {
// do something else
echo '';
}
}
?>
</td>
<?php
echo "<td>
<select name=operation>
<option value =0></option>";
include "../render/render_operation-selected.php";
echo "</select>
</td>";
?>
</tr>
<tr>
<td>تاريخ التحيين</td>
<td>
<?php
echo '' . $row['date'] . '';
?>
</td>
<td style="direction:rtl;">
<?php echo "<input type='text' value='".date('o/m/d')."' style='width: 148px;' name='date' readonly=readonly>";?>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="تغيير" class="brd"></td>
<td align="center">
<input type="submit" value="إلغاء" onClick="history.go(-1);return true;" class="bbl">
</td>
</tr>
</table>
<?php
}
// connect to the database
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['serie']))
{
// get form data, making sure it is valid
$serie = $_POST['serie'];
$user_id = mysql_real_escape_string(htmlspecialchars($_POST['user_id']));
$journee = mysql_real_escape_string(htmlspecialchars($_POST['journee']));
$titre = mysql_real_escape_string(htmlspecialchars($_POST['titre']));
$region = mysql_real_escape_string(htmlspecialchars($_POST['region']));
$role = mysql_real_escape_string(htmlspecialchars($_POST['role']));
$operation = mysql_real_escape_string(htmlspecialchars($_POST['operation']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
// check that firstname/lastname fields are both filled in
if ($user_id == '' || $journee == '' || $titre == '' || $region == '' || $role == '' || $operation =='' || $date =='')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($serie, $user_id, $journee, $titre, $region, $role , $operation, $date, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE taches_redaction SET user_id='$user_id', journee='$journee', titre='$titre', region='$region', role='$role', operation='$operation', date='$date' WHERE serie='$serie'")
/* frere update */
/*
AND
mysql_query("INSERT copie SET user_id='$user_id', journee='$journee', titre='$titre', region='$region', role='$role', operation='$operation', date='$date'")
AND
mysql_query("INSERT colla SET user_id='$user_id', journee='$journee', titre='$titre', region='$region', role='$role', operation='$operation', date='$date'")
AND
mysql_query("INSERT archive SET user_id='$user_id', journee='$journee', titre='$titre', region='$region', role='$role', operation='$operation', date='$date'")
*/
or die(mysql_error());
// once saved, redirect back to the view page
/* header('Refresh: 2; URL = taches_view.php');
*/
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['serie']) && is_numeric($_GET['serie']) && $_GET['serie'] > 0)
{
// query db
$serie = $_GET['serie'];
$result = mysql_query("SELECT * FROM taches_redaction WHERE serie=$serie")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$serie = $row['serie'];
$user_id = $row['user_id'];
$journee = $row['journee'];
$titre = $row['titre'];
$region = $row['region'];
$role = $row['role'];
$operation = $row['operation'];
$date = $row['date'];
// show form
renderForm($serie, $user_id, $journee, $titre, $region, $role, $operation, $date, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
<!-- end .content --></div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<?php
include '../theme/footer-global.php';
?>
I need help with enableing/disableing parts of my website.
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
<input type="checkbox" name="SRS" onchange="document.getElementById('e-d-check').submit()">SkiRegionSimulator 2012</input>
<input type="checkbox" name="Bilder" onchange="document.getElementById('e-d-check').submit()">Bilder</input>
<input type="checkbox" name="KWS" onchange="document.getElementById('e-d-check').submit()">Klimawandel-Stunde</input>
</form>
<table>
<tr>
<th>Name</th>
<th>Größe</th>
<th>Datum</th>
<th>Download</th>
</tr>
<?php
if (isset($_POST['SRS'])) {
echo 'Disabled SkiRegionSimulator 2012';
} else {
include'SRS.php';
}
?>
Everytime I uncheck the box it checks itself again...
Check if this works for you:
<?php
$disabled = array();
$disabled['SRS'] = isset($_POST['SRS']) && (int)$_POST['SRS'] === 1;
$disabled['Bilder'] = isset($_POST['Bilder']) && (int)$_POST['Bilder'] === 1;
$disabled['KWS'] = isset($_POST['KWS']) && (int)$_POST['KWS'] === 1;
?>
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
<input type="checkbox" value="1" <?php if($disabled['SRS'] === true) echo "checked"; ?> name="SRS" onchange="document.getElementById('e-d-check').submit()">SkiRegionSimulator 2012</input>
<input type="checkbox" value="1" <?php if($disabled['Bilder'] === true) echo "checked"; ?> name="Bilder" onchange="document.getElementById('e-d-check').submit()">Bilder</input>
<input type="checkbox" value="1" <?php if($disabled['KWS'] === true) echo "checked"; ?> name="KWS" onchange="document.getElementById('e-d-check').submit()">Klimawandel-Stunde</input>
</form>
<table>
<tr>
<th>Name</th>
<th>Größe</th>
<th>Datum</th>
<th>Download</th>
</tr>
<?php
if ($disabled['SRS'] === true) {
echo 'Disabled SkiRegionSimulator 2012';
} else {
include'SRS.php';
}
?>
We control what checkbox is checked by using the (guess!) checked attribute. If you are using XHTML then change checked to checked="checked"
A different but similar approach with submit onclick.
<?php
$check_SRS = "";
$check_Bilder = "";
$check_KWS = "";
if (isset($_POST['SRS']) && intval($_POST['SRS']) == 1) { $check_SRS = "checked";}
if (isset($_POST['Bilder']) && intval($_POST['Bilder']) == 1) { $check_Bilder = "checked";}
if (isset($_POST['KWS']) && intval($_POST['KWS']) == 1) { $check_KWS = "checked";}
?>
<p>Disabled Sections:</p>
<form id="e-d-check" method="post">
<input type="checkbox" name="SRS" value="1" onclick="submit()" <?php echo htmlspecialchars($check_SRS); ?>>SkiRegionSimulator 2012</input>
<input type="checkbox" name="Bilder" value="1" onclick="submit()" <?php echo htmlspecialchars($check_Bilder); ?>>Bilder</input>
<input type="checkbox" name="KWS" value="1" onclick="submit()" <?php echo htmlspecialchars($check_KWS); ?>>Klimawandel-Stunde</input>
</form>
<table>
<tr>
<th>Name</th>
<th>Größe</th>
<th>Datum</th>
<th>Download</th>
</tr>
<?php
if (isset($_POST['SRS']) && intval($_POST['SRS']) == 1) {
echo 'Disabled SkiRegionSimulator 2012';
} else {
include'SRS.php';
}
?>
A version of elseif statements to accommodate what are assumed to be the other includes depending on the state of the checkboxes - if one only can be checked:
<?php
if(intval($_POST['SRS']) == 1){
echo 'Disabled SkiRegionSimulator 2012';
}elseif(intval($_POST['SRS']) != 1){
include'SRS.php';
}elseif(intval($_POST['Bilder']) == 1){
echo 'Disabled Bilder';
}elseif(intval($_POST['Bilder']) != 1){
include'Bilder.php';
}elseif(intval($_POST['KWS']) == 1){
echo 'Disabled KWS';
}elseif(intval($_POST['KWS']) != 1){
include'KWS.php';
}
?>
if more than one can be checked:
<?php
if(intval($_POST['SRS']) == 1){
echo 'Disabled SkiRegionSimulator 2012';
}else{
include'SRS.php';
}
if(intval($_POST['Bilder']) == 1){
echo 'Disabled Bilder';
}else{
include'Bilder.php';
}
if(intval($_POST['KWS']) == 1){
echo 'Disabled KWS';
}else{
include'KWS.php';
}
?>
This php file is using server side validation via post method as the user enters data into the input devices. The only problem I'm having is inserting the data into the customers table as it doesn't work. I know this because i have created test php file that displays all the customer tables content and the data the user enters is not there. Where have i gone wrong?
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 2 Task 12</title>
</head>
<body>
<?php
$conn = mysql_connect("localhost", "twa291", "......");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );
$sql = "SELECT * FROM customer";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
$ename = $elname = $ecus = $epcode = "";
$fnamecus = $lnamecus = $idcus = $pcde = "";
$error_report = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["customerid"])) {
$ecus = "Customer ID is required";
$error_report = true;
} else {
$idcus = input_t($_POST["customerid"]);
// check if numeric
if (preg_match("/[^0-9]/",$idcus)) {
$ecus = "Only numbers allowed";
$error_report = true;
}
if(strlen($idcus) != 6 && ($idcus) != null)
{
$ecus = "Customer ID must be 6 digits";
$error_report = true;
}
}
if (empty($_POST["customerfname"])) {
$ename = "First name is required";
$error_report = true;
} else {
$fnamecus= input_t($_POST["customerfname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z-]*$/",$fnamecus)) {
$ename = "Only alphabetic letters and hyphen";
$error_report = true;
}
if(strlen($fnamecus) > 20 && ($fnamecus) != null)
{
$ename = "First name can't be more that 20 characters long";
$error_report = true;
}
}
if (empty($_POST["customerlname"])) {
$elname = "Last name is required";
$error_report = true;
} else {
$lnamecus = input_t($_POST["customerlname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z-]*$/",$lnamecus)) {
$elname = "Only alphabetic letters and hyphen";
$error_report = true;
}
if(strlen($lnamecus) > 20 && ($lnamecus) != null)
{
$elname = "Last name can't be more that 20 characters long";
$error_report = true;
}
}
if (!is_null($_POST["postcode"])) {
$pcde = input_t($_POST["postcode"]);
// check if name only contains letters and whitespace
if (preg_match("/[^0-9]/",$pcde)) {
$epcode = "Only numbers allowed";
$error_report = true;
}
if(strlen($pcde) != 4 && ($pcde) != null)
{
$epcode = "Post code must be 4 digits";
$error_report = true;
}
}
}
if($error_report != true) {
$query="INSERT INTO customer (customerID, firstName, lastName, Address, suburb, state, postcode)
VALUES ('customerid', 'customerfname', ‘customerlname', 'customeraddress', 'suburb',
'state', 'postcode')";
$queryResult = mysql_query($query, $conn)
or die ('Problem with query' . mysql_error());
echo "correct";
}
function input_t($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h1>Customer Information Collection <br /></h1>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="custinfo" >
<table>
<tr>
<td><label for="customerid">Customer ID (integer value): </label></td>
<td><input type="text" id="customerid" name="customerid" size=11 value="<?php
echo $idcus;?>"/><span class="error">* <?php echo $ecus;?></span></td>
</tr>
<tr>
<td><label for="customerfname">Customer Frist Name: </label></td>
<td><input type="text" id="customerfname" name="customerfname" size=50 value="<?php
echo $fnamecus;?>"/><span class="error">* <?php echo $ename;?></span></td>
</tr>
<tr>
<td><label for="customerlname">Customer Last Name: </label></td>
<td><input type="text" id="customerlname" name="customerlname" size=50 value="<?php
echo $lnamecus;?>"/><span class="error">* <?php echo $elname;?></span></td>
</tr>
<tr>
<td><label for="customeraddress">Customer Address: </label></td>
<td><input type="text" id="customeraddress" name="customeraddress" size=65/></td>
<td><label for="suburb"> Suburb: </label></td>
<td><input type="text" id="suburb" name="suburb"/></td>
</tr>
<tr>
<td>
State:<select name="state" id="state">
<option value="select">--</option>
<option value="ACT">ACT</option>
<option value="NSW">NSW</option>
<option value="NT">NT</option>
<option value="QLD">QLD</option>
<option value="SA">SA</option>
<option value="TAS">TAS</option>
<option value="VIC">VIC</option>
<option value="WA">WA</option>
</select>
</td>
<td><label for="postcode"> Post Code: </label><input type="text" id="postcode"
name="postcode" size=4 value="<?php
echo $pcde;?>"/><span class="error"><?php echo $epcode;?></span></td>
</tr>
</table>
<p><input type="submit" value="Save Data"/> <input type="reset" value="Clear Form" />
</tr>
</form>
</body>
</html>
You need to call mysql_query on your $query -- right now you're just defining the $query object and then ignoring it for the rest of the page.
Add something like the following on the line before echo "correct";
$queryResult = mysql_query($query, $conn)
or die ('Problem with query' . mysql_error());
n.b. I'll echo #Ozmah's comment about looking into PDO or mysqli functions - learning the deprecated plain mysql functions will be of dubious value.
I have this whole code. Jumbled but what I'm trying to do is to have a drop down menu with selections from 1 -10. Then after selecting one, would spit out another form so that if there 3 were selected, would show three rows of fields (name, email). Validation included. What I was first "hard coded" on how many rows of fields to spit out but now what I wanted to do is to have a "selection" in dropdown menu so that the user has the ability to select.... Seem to work but doesn't process. Any PHP expert help? No db, no client-base, no smart alec. If you have time to help please do.
<!DOCTYPE html>
<html>
<head>
<title>PHP FORM </title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="container">
<?php
// Print some introductory text:
echo '<h2>Party Invitation Form</h2>
<p>Please enter list of people with first name, last name and email address to get an invitation by email.</p>';
if (isset($_POST['submit-invite'])) { //DROPDOWN MENU
$row = $_POST['invitee'];
// Check if the form has been submitted:
if (isset($_POST['submit'])) {
$problem = FALSE; // No problems so far.
// Check for each value...
for ($i = 1; $i < count($_POST['email']); $i++) {
if (empty($_POST['firstname'][$i])) {
$problem = TRUE;
echo '<input type="text" name="firstname[]" size="20" />';
}
if (empty($_POST['lastname'][$i])) {
$problem = TRUE;
}
if (empty($_POST['email'][$i]) || (substr_count($_POST['email'][$i], '#') != 1) ) {
$problem = TRUE;
}
}
if (!$problem) { // If there weren't any problems...
// Print a message:
echo '<p><b>Thank you for registering! We will send each one an invitation: <b> </b></p>';
for ($i = 0; $i < count($_POST['email']); $i++) {
$row = $i+1;
echo $row.": ".$_POST['firstname'][$i]." ".$_POST['lastname'][$i].", ".$_POST['email'][$i]." <br/>";
// Send the email:
$body = file_get_contents("Lab12_Obj1_email_template.txt");
$body = str_replace("#firstname#",$_POST['firstname'][$i],$body);
$body = str_replace("#lastname#",$_POST['lastname'][$i],$body);
$body = str_replace("#email#",$_POST['email'][$i],$body);
mail($_POST['email'][$i], 'Party Invitation', $body, 'From: jvicencio#johnvicencio.com');
}
// Clear the posted values:
$_POST = array();
} else { // Forgot a field.
echo '<p id="error">* Required field! Please try again. Thank you.</p>';
}
} // End of handle form IF.
//show form
?>
<form action="" method="post">
<table>
<tr style="font-weight:bold">
<td>First name:</td>
<td>Last name:</td>
<td>Email:</td>
</tr>
<?php for ($i = 1; $i <= $row; $i++) { ?>
<tr>
<td><?php if ($problem == TRUE) { echo '<p id="error">*'; } ?>
<? echo $i.': '; ?><input type="text" name="firstname[]" size="20" value="<?php if (isset($_POST['firstname'][$i])) { print htmlspecialchars($_POST['firstname'][$i]); } ?>" />
</td>
<td><?php if ($problem == TRUE) { echo '<p id="error">*'; } ?>
<input type="text" name="lastname[]" size="20" value="<?php if (isset($_POST['lastname'] [$i])) { print htmlspecialchars($_POST['lastname'][$i]); } ?>" />
</td>
<td><?php if ($problem == TRUE) { echo '<p id="error">*'; } ?><input type="text" name="email[]" size="20" value="<?php if (isset($_POST['email'][$i])) { print htmlspecialchars($_POST['email'][$i]); } ?>" />
</td>
</tr>
<?php } ?>
<tr><td><p><input type="submit" class="button" name="submit" value="Register!" /></td>
</tr>
</table>
</form>
<?php
}
else {
echo '
<form action="" method="post">
<select name="invitee">
<option value="">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
<option value="7">Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
<option value="10">Ten</option>
</select>
<input type="submit" class="button" name="submit-invite" value="Invite">
</form>
';
}
?>
</div>
</body>
</html>
You have to move the second form submit code outside of first form submit. Try this
<!DOCTYPE html>
<html>
<head>
<title>PHP FORM </title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="container">
<?php
// Print some introductory text:
echo '<h2>Party Invitation Form</h2>
<p>Please enter list of people with first name, last name and email address to get an invitation by email.</p>';
if (isset($_POST['submit-invite'])) { //DROPDOWN MENU
$row = $_POST['invitee'];
$problem = false; //always try to initialize variables, you may not run into unexpected warnings and problems.
//show form
?>
<form action="" method="post">
<table>
<tr style="font-weight:bold">
<td>First name:</td>
<td>Last name:</td>
<td>Email:</td>
</tr>
<?php for ($i = 1; $i <= $row; $i++) { ?>
<tr>
<td><?php if ($problem == TRUE) {
echo '<p id="error">*';
} ?>
<? echo $i . ': '; ?><input type="text" name="firstname[]" size="20"
value="<?php if (isset($_POST['firstname'][$i])) {
print htmlspecialchars($_POST['firstname'][$i]);
} ?>"/>
</td>
<td><?php if ($problem == TRUE) {
echo '<p id="error">*';
} ?>
<input type="text" name="lastname[]" size="20"
value="<?php if (isset($_POST['lastname'] [$i])) {
print htmlspecialchars($_POST['lastname'][$i]);
} ?>"/>
</td>
<td><?php if ($problem == TRUE) {
echo '<p id="error">*';
} ?><input type="text" name="email[]" size="20"
value="<?php if (isset($_POST['email'][$i])) {
print htmlspecialchars($_POST['email'][$i]);
} ?>"/>
</td>
</tr>
<?php } ?>
<tr>
<td><p><input type="submit" class="button" name="submit" value="Register!"/></td>
</tr>
</table>
</form>
<?php
} // moved second for submit to here
elseif (isset($_POST['submit'])) { // Check if the form has been submitted:
$problem = FALSE; // No problems so far.
// Check for each value...
for ($i = 1; $i < count($_POST['email']); $i++) {
if (empty($_POST['firstname'][$i])) {
$problem = TRUE;
echo '<input type="text" name="firstname[]" size="20" />';
}
if (empty($_POST['lastname'][$i])) {
$problem = TRUE;
}
if (empty($_POST['email'][$i]) || (substr_count($_POST['email'][$i], '#') != 1)) {
$problem = TRUE;
}
}
if (!$problem) { // If there weren't any problems...
// Print a message:
echo '<p><b>Thank you for registering! We will send each one an invitation: <b> </b></p>';
for ($i = 0; $i < count($_POST['email']); $i++) {
$row = $i + 1;
echo $row . ": " . $_POST['firstname'][$i] . " " . $_POST['lastname'][$i] . ", " . $_POST['email'][$i] . " <br/>";
// Send the email:
$body = file_get_contents("Lab12_Obj1_email_template.txt");
$body = str_replace("#firstname#", $_POST['firstname'][$i], $body);
$body = str_replace("#lastname#", $_POST['lastname'][$i], $body);
$body = str_replace("#email#", $_POST['email'][$i], $body);
mail($_POST['email'][$i], 'Party Invitation', $body, 'From: jvicencio#johnvicencio.com');
}
// Clear the posted values:
$_POST = array();
} else { // Forgot a field.
echo '<p id="error">* Required field! Please try again. Thank you.</p>';
}
} // End of handle form IF.
else {
echo '
<form action="" method="post">
<select name="invitee">
<option value="">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
<option value="7">Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
<option value="10">Ten</option>
</select>
<input type="submit" class="button" name="submit-invite" value="Invite">
</form>
';
}
?>
</div>
</body>
</html>
I do not know what im doing wrong. I have been looking through other forums that say my problem might be related to not having closed curly braces closed or a short php tag <? .... I have none of these as far as I can tell. This is a form that lets you know if any fields are left blank.
<?php
if (count($_POST) > 0)
{
function check_if_field_submitted($field_to_check)
{
if (isset($_POST[$field_to_check]) && $_POST[$field_to_check] != '')
{
return TRUE;
}
else
{
return "YOU MUST FILL IN THE $field_to_check FIELD!";
}
}
//--------------------------------------------------------
$error_messages = array();
//Validate the input
//Trim the fields
$_POST['first_name'] = trim($_POST['first_name']);
$_POST['last_name'] = trim($_POST['last_name']);
$_POST['comments'] = trim($_POST['comments']);
$_POST['first_name'] = strip_tags($_POST['first_name']);
$_POST['last_name'] = strip_tags($_POST['last_name']);
$_POST['comments'] = strip_tags($_POST['comments']);
//Required fields:
if (check_if_field_submitted('first_name') !== TRUE)
{
$error_messages[] = check_if_field_submitted('first_name');
}
if (check_if_field_submitted('last_name') !== TRUE)
{
$error_messages[] = check_if_field_submitted('last_name');
}
if (check_if_field_submitted('hobbies') !== TRUE)
{
$error_messages[] = check_if_field_submitted('hobbies');
}
if (check_if_field_submitted('university') !== TRUE)
{
$error_messages[] = check_if_field_submitted('university');
}
if (check_if_field_submitted('year') !== TRUE)
{
$error_messages[] = check_if_field_submitted('year');
}
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
if (count($error_messages) < 1)
{
header("Location: success.php");
}
}
?>
<DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($error_messages) && count($error_messages) > 0)
{
echo "<ul>";
foreach($error_messages as $message)
{
echo "<li>$message</li>";
}
echo "</ul>";
}
?>
<h1>Register or Fail</h1>
<form method="post" action="index.php">
<fieldset>
<label>First Name</label>
<input type='text' name='first_name' value="<?php if(isset($_POST['first_name'])) { echo $_POST['first_name']; } ;?>" />
<label>Last Name</label>
<input type='text' name='last_name' value='<?php if(isset($_POST['last_name'])) { echo $_POST['last_name']; } ;?>'/>
</fieldset>
<fieldset>
<label>What are your hobbies?</label>
<input type="checkbox" name="hobbies" value="movies" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'movies') { echo "checked='checked'"; } ?> /> Movies
<input type="checkbox" name="hobbies" value="sports" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'sports') { echo "checked='checked'"; } ?> /> Sports
<input type="checkbox" name="hobbies" value="books" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'books') { echo "checked='checked'"; } ?> /> Books
<input type="checkbox" name="hobbies" value="vgames" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'vgames') { echo "checked='checked'"; } ?> /> Video Games
<input type="checkbox" name="hobbies" value="science" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'science') { echo "checked='checked'"; } ?> /> FOR SCIENCE!
</fieldset>
<fieldset>
<label>What year are you?</label>
<input type="radio" name="year" value="1" <?php if(isset($_POST['year']) && $_POST['year'] == '1') { echo "checked='checked'"; } ?> /> Freshman
<input type="radio" name="year" value="2" <?php if(isset($_POST['year']) && $_POST['year'] == '2') { echo "checked='checked'"; } ?> /> Sophomore
<input type="radio" name="year" value="3" <?php if(isset($_POST['year']) && $_POST['year'] == '3') { echo "checked='checked'"; } ?> /> Junior
<input type="radio" name="year" value="4" <?php if(isset($_POST['year']) && $_POST['year'] == '4') { echo "checked='checked'"; } ?> /> Senior
</fieldset>
<fieldset>
<label>What university are you attending?</label>
<select name="university">
<option value="" <?php if(isset($_POST['university']) && $_POST['university'] == '') { echo "selected='selected'"; } ?> >Please Select an Option</option>
<option value="1" <?php if(isset($_POST['university']) && $_POST['university'] == '1') { echo "selected='selected'"; } ?> >Florida State University</option>
<option value="2" <?php if(isset($_POST['university']) && $_POST['university'] == '2') { echo "selected='selected'"; } ?> >University of Florida</option>
<option value="3" <?php if(isset($_POST['university']) && $_POST['university'] == '3') { echo "selected='selected'"; } ?> >University of Central Florida</option>
<option value="4" <?php if(isset($_POST['university']) && $_POST['university'] == '4') { echo "selected='selected'"; } ?> >University of Miami</option>
</select>
</fieldset>
<fieldset>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>
Problem:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
if (count($error_messages) < 1)
{
header("Location: success.php");
}
}
Answer:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
}
if (count($error_messages) < 1)
{
header("Location: success.php");
}
}
If I were you I would find an IDE with bracket matching/highlighting.
It looks like you're missing a closing brace on one of your if statements.
I think you want to change these lines:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
to this:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
}