Jump to content

Need to add a field to PHP/HTML Form


vigiw

Recommended Posts

Hello,

I currently have a great and working setup for a form sending data to a MySQL database and displaying it in another file (Big thanks to Corbin on these Forums!), but I recently decided that I just want to add one more text box field to the form and database.  We will call this field4.

field4 is going to be placed between field1 and field2 -- the order being:

field1
field4
field2
field3

Only because field4 was just thought of and it best fits in that position.  Here's my current file called submit.php:

submit.php
[code]<?
include("db.php");

$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
$field3 = $_POST['field3'];
if($_POST['sub'] == "yes") {
if($field1 == "") { $error .= "You did not put any information for field1<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field1) > 30) { $error .= "Field1 is too long.<BR>"; }

if($field2 == "") { $error .= "You did not put any information for field2<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field2) > 999) { $error .= "Field2 is too long.<BR>"; }

if($field3 == "") { $error .= "You did not put any information for field3<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field3) > 256) { $error .= "Field3 is too long<BR>."; }
}
if($_GET['success'] != "yes") { ?>
<form action="submit.php" method=POST>
<font size="2" face="Arial">Alert Type: </font><font face="Arial">
<input type="text" maxlength="30" name="field1" <? if(isset($field1)) { echo "value=\"" . $field1 . "\""; } ?>><font size="2">
Choices: Severe Weather Watch -or- Severe Weather Watch Update -or- Severe
Weather Watch Correction (Use choice 1 if it is just a normal Severe Weather
Watch
issuance; use choice 2 if it is an update from an earlier Severe Weather Watch; </font></font><font size="2" face="Arial">
use choice 3 if you are correcting a </font><font size="2" face="Arial">
Severe Weather Watch that you issued earlier)<BR>
Alert Text:&nbsp; </font><font face="Arial">
<textarea name="field2" rows="9" cols="78">Type in the alert, detailed, here.  Text box can fit up to 999 characters.</textarea></font><font size="2" face="Arial"><BR>
Forecaster: </font><font face="Arial">
<input type="text" maxlength="256" name="field3" <? if(isset($field3)) { echo "value=\"" . $field3 . "\""; } ?>><font size="2">
(Your first and last name)</font></font><font size="2" face="Arial"><BR>
</font><font face="Arial">
<input type="hidden" name="sub" value="yes">
<input type="submit" value="Send"></font><p><font face="Arial"> **KEEP IN MIND:
Severe Weather Watches are not official alerts issued by NOAA!&nbsp; Also, we
issue these only as needed.&nbsp; Be conservative, as with all other alerts
issued via VWFN.</font><font size="2" face="Arial"> </font>
</p>
</form>
<?
}
/*
note: .= defines a variable or adds onto it if it already exists... for example
$var1 = "hello";
$var1 .= " my name is corbin";
echo $var1;
would out put hello my name is corbin
*/
if(isset($error)) {
echo "There was one or more error!<BR>";
echo $error;
}
else {
if(($_GET['success'] != "yes") && ($_POST['sub'] == "yes")) { // checks to see if $error is set of if the GET variable of success does not equal yes
$field1 = htmlspecialchars($field1);// if $field does not return an error it replaces any thing that could be interpetted as a browser as html
$field2 = htmlspecialchars($field2);
$field3 = htmlspecialchars($field3);
$q = mysql_query("INSERT INTO `" . $table_name . "` (`field1` , `field2` , `field3`) VALUES ('" . $field1 . "', '" . $field2 . "', '" . $field3 ."')");
if($q) { //what to do if the mysql query is successful
$error = NULL;
?>
<meta http-equiv="refresh" content="0;url=<? echo $_SERVER['HTTP_SELF'] . "?success=yes"; ?>" ?>" ?>" ?>" ?>">
<?
//by forwarding to its self the page will clear its post values so if a user hits refresh its not inserted into the database again...
}  //the meta tag has it forward it to this page cept with the $_GET['success'] tag set
}
elseif($_GET['success'] == "yes") { //what to do if $_GET['success'] does equal yes
echo "Severe Weather Watch successfully issued.<BR> View it <a href=\"http://www.vigilantweather.com/forecast/viewseverewatch/bos_view.php\">Here</a>.";
}
}
mysql_close(); //closes the link to mysql}
?>[/code]

It seems easy since all that has to be done is add one field to wherever in this file it needs to be shown.

field4 is already added to the database and it is a TEXT field.

Could anyone please help me out.  I don't want to ruin this file, and I just one that one more text field added as described in this message. And I am quite new to PHP.

Once again this file IS working, I just want to add that one field! :)

Thanks in advance! ;D
Link to comment
Share on other sites

[code]
$feild4 = $_POST['feild4'];
$feild4_mx_length = "100";//feild4 max length
if($field4 == "") { $error .= "You did not put any information for field2<BR>"; } //checks to see if the user filled in field1, or if they put in too much information, and defines the variable error if they did
if(strlen($field4) > $feild4_mx_length) { $error .= "Field4 is too long.<BR>"; }
and just enter it in ur database
insert into (feild1,feild2,field3,field4) values ('$field1','$field2','$field3','$field4')
[/code]
sorry i didnt read your whole code im not sure which rows you want to fill in but hopefully you get my point
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.