Hi
I'm still new to this but I think I'm making progress. Below is a functioning script that inserts 4 fields into a database. After advice from a previous post the form is being processed on the same page. The insert into database works fine but I only want to display the 'thank you message' after data has been entered. Currently it is displayed all the time.
Here's the script:
[PHP]// connect to server & open database
include '../include/config.php';
include '../include/opendb.php';
$Name = $_POST['Name'];
$ADno = $_POST['ADno'];
$RecFriend = $_POST['RecFriend'];
$RecEmail = $_POST['RecEmail'];
// add info into the database
mysql_query($query);
$query="INSERT INTO recommended (Name, ADno, RecFriend, RecEmail) VALUES ('$Name','$ADno','$RecFriend','$RecEmail')";
if(!mysql_query($query)) die(mysql_error());
echo "Thank you for your recommendation!";
//close db
include '../include/closedb.php';
[/PHP]
Can someone give me an idea where to go from here and confirm (hopefully) that what I have done so far is solid.
Thanks people.
5 2583
Now you state that you want the thank you in the same page as the form. But you are showing only the first part of your script!! See my reply to you at http://www.thescripts.com/forum/post2238469-3.html that gives the form structure.
So if you want advice here, you'd better show the entire script here, not just part of it, because that doesn't say anything on how it is invoked or executed.
Ronald :cool:
Sorry... as I said, I'm finding my way around with this. Here's the form. Thanks for looking.
[HTML]
<div class="formRec">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="Recommend Friend" onsubmit="MM_validateForm('Name','','R','ADno','', 'R','RecFriend','','R','RecEmail','','RisEmail');r eturn document.MM_returnValue">
<div align="right"><span class="style1">
<h2>
<?php include("RecommendEcho.php"); ?></h2>
*Your UserName: <br />
<input name="Name" type="text" maxlength="30" value="<?php print $slusername; ?>"/>
<br />
<br/>
*Your AD Number:<br />
<input name="ADno" type="text" maxlength="30" value="<?php print $slcustom6; ?>" />
<br />
<br/>
*Friend's Name:<br />
<input name="RecFriend" type="text" maxlength="50"/>
<br/>
<br/>
*Friend's Email:<br />
<input name="RecEmail" type="text" maxlength="50" />
<br/>
<br/>
</span>
*All fields are required
<input name="Submit" type="Submit" value="send" />
<br />
<h2> </h2>
</div></form>[/HTML]
You still show your code script in 2 pieces, while it is only ONE script.
You must also sanitize your input data and check the validity of the input fields. You say on your form that all fields are mandatory, but how do you force that?? That is also something you must check.
So here is the reworked code:[php]<?php
// connect to server & open database
include '../include/config.php';
if (isset($_POST['Submit'])) {
$Name = trim(strip_tags($_POST['Name']));
$ADno = trim(strip_tags($_POST['ADno']));
$RecFriend = trim(strip_tags($_POST['RecFriend']));
$RecEmail = trim(strip_tags($_POST['RecEmail']));
//
// ------------
// you'd better verify your input fields before inserting them in the database
// ------------
// ...........
// connect to server and database
include '../include/opendb.php';
// add info into the database
$query="INSERT INTO recommended (Name, ADno, RecFriend, RecEmail) VALUES ('$Name','$ADno','$RecFriend','$RecEmail')";
$res = mysql_query($query)
or die("Insert error: " . mysql_error());
echo "Thank you for your recommendation!";
//close db
include '../include/closedb.php';
}
else {
?>
<div class="formRec">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="Recommend Friend" onsubmit="MM_validateForm('Name','','R','ADno','', 'R','RecFr iend','','R','RecEmail','','RisEmail');return document.MM_returnValue">
<div align="right"><span class="style1">
<h2>
<?php include("RecommendEcho.php"); ?></h2>
*Your UserName: <br />
<input name="Name" type="text" maxlength="30" value="<?php print $slusername; ?>"/>
<br />
*Your AD Number:<br />
<input name="ADno" type="text" maxlength="30" value="<?php print $slcustom6; ?>" />
<br />
*Friend's Name:<br />
<input name="RecFriend" type="text" maxlength="50"/>
<br/>
<br/>
*Friend's Email:<br />
<input name="RecEmail" type="text" maxlength="50" />
<br/>
</span>
*All fields are required
<input name="Submit" type="Submit" value="send" />
<br />
<h2> </h2>
</div>
</form>
</div>
<?php } ?>[/php]
Ronald :cool:
Why don't you use a simple "if(...){echo...}"?
Thanks for that - I will try to implement it. I have the following validation already in place - which was generated by dreamweaver's inbuilt form validator - it seems to function fine but probably isn't the best way to do this... but hey, there's only so many hours in a day!..
I appreciate the advice!
[HTML]function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_valida teForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}[/HTML]
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lawrence |
last post by:
A very strange bug. www.monkeyclaus.org is run by a cms I'm
developing. One of types of users we allow is "justTestingTheSite", a
type of user I developed to give demo's to prospective clients. The...
|
by: Bryan Wood |
last post by:
I am developing a regisration page that will present a set of check
boxes to the viewer. This list of checkboxes is developed from a list
in a database so the amount and names of the boxes will...
|
by: Ian Rastall |
last post by:
Ah, I hope it's okay to keep posting questions, and sorry for the
vague subject line. This is my problem: I've got a database set
up with 7 fields: ID, Album, Year, Type, Songs, Members,...
|
by: josh.kuo |
last post by:
Sorry about the subject, I can't think of a better one.
I recently wrote some PHP classes that I think might be of interest to
this group. Since I have been reaping the benefits of reading news...
|
by: bb nicole |
last post by:
Below is my job post code, it cannot post into database... WHAT IS THE PROBLEM OF IT??
<?php
session_start();
ob_start();
//connect to server and select database
|
by: webrod |
last post by:
Hi,
I have some php pages with a lot of HTML code.
I am looking for a HTML validator tool (like TIDY).
TIDY is not good enough with PHP tags (it removes a lot of php code).
Do you have any...
|
by: dream2rule |
last post by:
Hello All,
I am trying to validate multiple checkboxes whose values are stored in an array using php. I have been trying from a really long time but nothing's working out.
Can anyone help?
...
|
by: Mike Hofer |
last post by:
Please forgive the cross-post to multiple forums. I did it
intentionally, but I *think* it was appropriate given the nature of my
question.
I'm working on an open source code library to help...
|
by: pgt |
last post by:
I have a working couple of pages (form submits 2 variables to the second
page using GET).
page1 has two dropdowns (generated from MySQL db).
page2 retrieves info from the database accordingly,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |