Forum Moderators: coopster

Message Too Old, No Replies

Drop Down Option Value Authentication

Drop Down Option Value Authentication

         

ecnaralc

1:52 am on Oct 9, 2009 (gmt 0)

10+ Year Member



Hi Guys.
I have a drop down box, which if the user forgets to select a category from I want an error message to say they need to select a category, at the moment a 0 is automatically inserted on form submit. Below is the code.
I am not certain how to go about this, anyone have any ideas.
Thanks in advance
Wayne.

<select name="section" id="section">
<option value="0">Select Section</option>
<?PHP
// Generate a drop-down list of sections.
$result = $connector->query('SELECT ID,name FROM cmssections ORDER BY name');
// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){
echo '<option value="'. $row['ID'].'ttt11ttt'.$row['name'].'">'.$row['name'].'</option>';
}
?>
</select>

d40sithui

11:53 am on Oct 9, 2009 (gmt 0)

10+ Year Member



On your submit script you would check the $_POST['section'] variable to see what kind of data is contained within it. If it is a 0, empty, a bogus number, or otherwise an nonexistent ID, you would capture the error and handle it as you please. A series of nested if/else statements will do, but of course there are other methods. The simplest way is to create a function to check for a nonempty integer. You'd also want to do a SQL check of the ID to see if it exists in the db.

andrewsmd

1:32 pm on Oct 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like this
<html>
<form name="form" method="post">
<select name="section">
<option value="0">Please Select</option>
<option value="1">Option 1</option>
<option value="2">Option 2 </option>
</select><br>
<input type="submit" name="submit" value="submit" />
</form>
<?php

//if they click the submit button
if(isset($_POST['submit'])){

if($_POST['section'] == 0){

echo("error please select an option");

}//if
elseif($_POST['section'] == 1){

echo("Option 1");

}//elseif
elseif($_POST['section'] == 2){

echo("Option 2");

}//elseif

}//if isset

?>

</html>

ecnaralc

12:24 am on Oct 10, 2009 (gmt 0)

10+ Year Member



Hello.. Thanks for the respnse, but I think I need to give you the form code.

<?php
$connector = new DbConnector();
if(isset($_POST['save']))
{
$title = $_POST['title'];
$menutitle = $_POST['menutitle'];
$tagline = $_POST['tagline'];
// HERE we have two new variables now that show id and name, you can try and echo them out to see if all is ok
// Of course, you would change the code down in $query from $section to $section_id and add one for $section_name
$section_parts = explode("ttt11ttt",$_POST['section']);
$section_id = $section_parts[0];
$section_name = $section_parts[1];
$section = $_POST['section'];
$content = $_POST['content'];
$updated = $_POST['updated'];
if(!get_magic_quotes_gpc())
{
$title = mysql_real_escape_string($title);
$menutitle = mysql_real_escape_string($menutitle);
$tagline = mysql_real_escape_string($tagline);
// $section = addslashes($section);
$content = mysql_real_escape_string($content);
$updated = mysql_real_escape_string($updated);
}
$query = "INSERT INTO articles (title, menutitle, content, tagline, section, sectionname, timestamp, updated) VALUES ('$title', '$menutitle', '$content', '$tagline', '$section_id', '$section_name', NOW(), NOW())";
mysql_query($query) or die('Error ,query failed');
echo "Article '$title' added";
}
?>
<form method="post">
<table width="700" border="0" cellpadding="2" cellspacing="1" class="box" align="center">
<tr>
<td width="100">Article Title</td>
<td><input size="81" maxlength="80" name="title" type="text" class="box" id="title"></td>
</tr>
<tr>
<td width="100">Menu Title</td>
<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle"></td>
</tr>
<tr>
<td width="100">Tagline</td>
<td><input size="81" maxlength="250" name="tagline" type="text" class="box" id="tagline"></td>
</tr>
<tr>
<td width="100">Section</td>
<td><select name="section" id="section">
<option value="0">Select Section</option>
<?PHP
// Generate a drop-down list of sections.
$result = $connector->query('SELECT ID,name FROM cmssections ORDER BY name');
// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){
echo '<option value="'. $row['ID'].'ttt11ttt'.$row['name'].'">'.$row['name'].'</option>';
}
?>
</select></td>
</tr>

<tr>
<td width="100">Content</td>
<td><textarea name="content" cols="115" rows="35" class="box" id="content"></textarea></td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="save" type="submit" class="box" id="save" value="Save Article"></td>
</tr>
</table>
</form>

TheMadScientist

1:31 am on Oct 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<?php
$connector = new DbConnector();
if(isset($_POST['save']) && isset($_POST['section']) && !empty($_POST['section']))
{
$title = $_POST['title'];
$menutitle = $_POST['menutitle'];
$tagline = $_POST['tagline'];
// HERE we have two new variables now that show id and name, you can try and echo them out to see if all is ok
// Of course, you would change the code down in $query from $section to $section_id and add one for $section_name
$section_parts = explode("ttt11ttt",$_POST['section']);
$section_id = $section_parts[0];
$section_name = $section_parts[1];
$section = $_POST['section'];
$content = $_POST['content'];
$updated = $_POST['updated'];
if(!get_magic_quotes_gpc())
{
$title = mysql_real_escape_string($title);
$menutitle = mysql_real_escape_string($menutitle);
$tagline = mysql_real_escape_string($tagline);
// $section = addslashes($section);
$content = mysql_real_escape_string($content);
$updated = mysql_real_escape_string($updated);
}
$query = "INSERT INTO articles (title, menutitle, content, tagline, section, sectionname, timestamp, updated) VALUES ('$title', '$menutitle', '$content', '$tagline', '$section_id', '$section_name', NOW(), NOW())";
mysql_query($query) or die('Error ,query failed');
echo "Article '$title' added";
}

elseif(isset($_POST['save']) && (!isset$_POST['section']) ¦¦ empty$_POST['section']))) {
echo "Please select a section";

/* set your form below to echo back the POST info as values, or checked, or whatever so they don't have to fill out the whole form again... You can use <?= "value=\"".$_POST['name']."\" ?>*/
?>

<form method="post">
<table width="700" border="0" cellpadding="2" cellspacing="1" class="box" align="center">
<tr>
<td width="100">Article Title</td>
<td><input size="81" maxlength="80" name="title" type="text" class="box" id="title"></td>
</tr>
<tr>
<td width="100">Menu Title</td>
<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle"></td>
</tr>
<tr>
<td width="100">Tagline</td>
<td><input size="81" maxlength="250" name="tagline" type="text" class="box" id="tagline"></td>
</tr>
<tr>
<td width="100">Section</td>
<td><select name="section" id="section">
<option value="">Select Section</option>

<?PHP
// Generate a drop-down list of sections.
$result = $connector->query('SELECT ID,name FROM cmssections ORDER BY name');
// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){
echo '<option value="'. $row['ID'].'ttt11ttt'.$row['name'].'">'.$row['name'].'</option>';
}
?>
</select></td>
</tr>

<tr>
<td width="100">Content</td>
<td><textarea name="content" cols="115" rows="35" class="box" id="content"></textarea></td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="save" type="submit" class="box" id="save" value="Save Article"></td>
</tr>
</table>
</form>
<?php } ?>

ecnaralc

12:26 am on Oct 11, 2009 (gmt 0)

10+ Year Member



Hi! TheMadScientist.
I tried your code, but I get an "Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in " on that line of code... seems like a bracket or two is missing, sorry about the inconvenience..
Wayne

elseif(isset($_POST['save']) && (!isset$_POST['section']) ¦¦ empty$_POST['section'])))

TheMadScientist

5:59 pm on Oct 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Did you get it fixed yet?

It's just a copy and paste error which should have been fairly easy to find... I pasted over a ( on isset() and empty()) (I make those errors here all the time). I'm guessing you found it, but if not, here's the correction:

elseif(isset($_POST['save']) && (!isset($_POST['section']) ¦¦ empty($_POST['section']))) {

Make sure you change from a broken bar to a regular one before running the code.

ecnaralc

1:58 am on Oct 12, 2009 (gmt 0)

10+ Year Member



Hi! TheMadScientist, okay I have got the form working, upto a point, it clears the form on submit if no category is selected.
I am uncertain where to place the following bit of code, sorry I am a bit slow at this.
<?= "value=\"".$_POST['name']."\" ?>

Also my form is blank when I place this at the bottom of the form <?php } ?>
Everything works when I place "}" after echo "Please select a section"; is that correct or not.
Regards
Wayne

TheMadScientist

10:30 pm on Oct 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You would put this: <?= "value=\"".$_POST['name']."\" ?> like this:

(['name'] would be change to the name of the field, so in this case you would change it to <?= "value=\"".$_POST['menutitle']."\" ?>)

<input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle" <?= "value=\"".$_POST['name']."\" ?>>

As to where to put: <?php } ?> where it works within your entire script is best... Some of it depends on the structure of the whole script, and if it works after the 'echo' you noted, then that's where you should put it.

<added>
I don't know if you are 'scrubbing' POSTed values within your script, but it's always a good idea and you might want to use html_entities() when you echo back the value, like this: <?= "value=\"".html_entities($_POST['name'])."\" ?>
</added>

ecnaralc

1:01 am on Oct 15, 2009 (gmt 0)

10+ Year Member



Hello again TheMadScientist..
I am placing the code exactly as shown below...
But the form is still not echoing the data I have filled.
Not sure if you can tell me whats wrong. Also the code after the the first line of code below all turns red.

<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle" <?= "value=\"".$_POST['menutitle']."\" ?> </td>

<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle" <?= "value=\"".html_entities($_POST['menutitle'])."\" ?> </td>

TheMadScientist

3:26 am on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<?= "value=\"".$_POST['menutitle']."\""; ?>

I left a quote off... It's one of the problems I have when coding here. No color. They're usually silly little mistakes I would easily and obviously notice when coding in color, but for some reason here I often forget the ) or a quote or something else simple, that's usually easy to find.

Sorry.

ecnaralc

7:40 am on Oct 15, 2009 (gmt 0)

10+ Year Member



Sorry Man this is still not happenning, I do not know how to look at it now. Would be nice to make it work. The </td> now turns orange. The code is below how I am using it, I was wondering how it knows which field to look at, does it look at the name or id.
Regards
Wayne
<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle" <?= "value=\"".$_POST['menutitle']."\""; ?> </td>

TheMadScientist

8:26 am on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's no closing tag on the text box...

<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle" <?= "value=\"".$_POST['menutitle']."\""; ?> /></td>

ecnaralc

9:00 am on Oct 15, 2009 (gmt 0)

10+ Year Member



Sorry Mate does not work.
The closing /> shows on the form when I go to use it. I removed the / but the bracket still shows. And nothing is echoed. Is really strange.
Regards
Wayne

TheMadScientist

9:10 am on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hmmmmmm...

Not sure what to tell you.
I just threw this on my favorite test page and it worked fine. (I didn't feel like setting up a form to actually POST the information, but it does not seem to be the specific code, unless you are passing some type of html information, and it's always a good idea to do something like this anyway, so there are two working examples below.)

<?php $menuTitle="Testing"; ?>
<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle" <?= "value=\"".$menuTitle."\""; ?> /></td>

<?php $menuTitle="Testing"; ?>
<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle" <?= "value=\"".htmlspecialchars($menuTitle)."\""; ?> /></td>

My best guess is there is an error somewhere else in your code... Not on the specific line you are mentioning. It's really either got to be that or your POSTed information.

BTW: POST uses the name="" value, not the id.

EDITED: Changed to htmlspecialchars() rather than htmlentities()... I think it's better in this situation.

skinsey

9:39 am on Oct 15, 2009 (gmt 0)

10+ Year Member



<script language="javascript">
function validate()
{

if (document.form.section.value=="")
{
alert("Please select section!")
return false;

} return true;
} </script>

<form name="form" method="post" onSubmit="return validate()">
<select name="section" id="section">
<option value="">Select Section</option>

TheMadScientist

9:54 am on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hey skinsey,

That's a good JS answer, and it's usable, but it's also a good idea to code the PHP correctly as a 'backup' if the JS fails (EG the user has JS turned off and wants to submit the form anyway, and forgets then too), which is why I didn't recommend that one in the first place, but maybe it's something ecnaralc is interested in using as 'a first line of defense', so thanks for throwing it in here.

BTW: Welcome to WebmasterWorld!

ecnaralc

10:01 am on Oct 15, 2009 (gmt 0)

10+ Year Member



Thanks skinsey but that did not work either.
There is no other code than what is on my second submission, I am using tiny mce on the content field, which code I stripped before posting.

TheMadScientist, I tried your new stuff, and it does the same showing /> on the form for that box when I test it.
But still does not echo the info back. So maybe it could be tiny_mce, I have no ideas.

skinsey

10:14 am on Oct 15, 2009 (gmt 0)

10+ Year Member



I agree quick and dirty doesn't always work.

The correct coding should be.

echo "<input type=\"text\" name=\"menutitle\"value=\"".$_POST["menutitle"]."\">";

TheMadScientist

10:16 am on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



K, skinsey's answer needed a little bit of adjustment to work on your specific form (and still does), so I just made it a bit more obvious below: (You'll need to edit FormName and FieldName to the <form name=""> value of your form, and the <select name=""> values respectively.)

<script language="javascript">
function validate()
{

if (document.FormName.FieldName.value=="")
{
alert("Please select section!")
return false;

} return true;
} </script>

<form name="FormName" method="post" onSubmit="return validate()">
<select name="FieldName" id="section">
<option value="">Select Section</option>

##### @ ##### @ #####

Hope you get it working with this for now and can address the PHP later... Like I said before it's a good JS solution and usable, but it's *always* a good idea to have a server-side 'back-up' just in case someone finds away around your JS or is not running JS.

ecnaralc

10:21 am on Oct 15, 2009 (gmt 0)

10+ Year Member



I took out tiny mce, same thing.
Okay skinsey, I see your reply, and just tried your code enclosed in php brackets, and it echos that one line, now I'll do it on the rest. Thanks for everyones input so far.

TheMadScientist

10:27 am on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



NVM This if you got it working... Mine previous code's essentially the same, but it's easier to make an error, and skinsey's is faster to parse but that's a different lesson.

Hope you get it working... If you don't, try:

Put the following code on a blank PHP page:

<?php $menuTitle="Testing"; ?>
<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle" <?= "value=\"".htmlspecialchars($menuTitle)."\""; ?> /></td>

<?php
echo "<input type=\"text\" name=\"menutitle\"value=\"".$menuTitle."\">";
?>

When you are sure they both work correctly, copy and paste them to the section of the page you are currently working on.

Open the page (submit the form if you have to) and ensure they work.

Edit them to the following:
<?php $menuTitle=$_POST["menutitle"]; ?>
<td><input size="21" maxlength="20" name="menutitle" type="text" class="box" id="menutitle" <?= "value=\"".htmlspecialchars($menuTitle)."\""; ?> /></td>

<?php
echo "<input type=\"text\" name=\"menutitle\"value=\"".$menuTitle."\">";
?>

See if you get the correct value...

If not: print_r($_POST) at the top of the code before you assign any $_POST values to anything or manipulate it in any way and check to see if the values are as expected... If they are, your error is between the top of the code (print_r) and the place in your form where your are trying to echo back the information. If they are not, your error is in the form or the information you are posting. (You can do this prior to my first suggest if you feel like.)

[edited by: TheMadScientist at 10:32 am (utc) on Oct. 15, 2009]

ecnaralc

10:32 am on Oct 15, 2009 (gmt 0)

10+ Year Member



Okay it all works and echos everything back except I have lost my content box.
When I put in skinsey' code, how do I change the rest of it to make tiny mce and the contents box as per the old code please.

<td><textarea name="content" cols="115" rows="35" class="box" id="content"></textarea></td>

<?php echo "<input type=\"textarea\" name=\"content\"value=\"".$_POST["content"]."\">";?>

skinsey

10:35 am on Oct 15, 2009 (gmt 0)

10+ Year Member



<td><textarea name="content" cols="115" rows="35" class="box" id="content"></textarea></td>

<?php
echo "<textarea name=\"content\" cols=\"115\" rows=\"35\" class=\"box\" id=\"content\" value=\"".$_POST["content"]."\">";
?>

skinsey

10:36 am on Oct 15, 2009 (gmt 0)

10+ Year Member



Paste only this

<?php
echo "<textarea name=\"content\" cols=\"115\" rows=\"35\" class=\"box\" id=\"content\" value=\"".$_POST["content"]."\">";
?>

TheMadScientist

10:41 am on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's about ten different ways to answer that, and I'm going to give you a couple just so you get some idea of what different code looks like:

This one is the same as skinsey's
<?php echo "<td><textarea name=\"content\" cols=\"115\" rows=\"35\" class=\"box\" id=\"content\">".htmlspecialchars($_POST['content'])."</textarea></td>"; ?>

This one lets you look at a bit less PHP and see where you are in your table a bit easier:
<td>
<?php echo "<textarea name=\"content\" cols=\"115\" rows=\"35\" class=\"box\" id=\"content\">".htmlspecialchars($_POST['content'])."</textarea>"; ?>
</td>

(<?= "" ?> is short hand for <?php echo "" ?> ¦ Basically = replaces php and echo)

This one is in shorthand, so you can mainly deal with HTML
<td><textarea name="content" cols="115" rows="35" class="box" id="content"><?= htmlspecialchars($_POST['content']) ?></textarea></td>

Accumulation and concatenation is the faster way to do all this stuff though, but is the most work when writing, because you really need to move all the html into php variables to use it:

$form="<td><textarea name=\"content\" cols=\"115\" rows=\"35\" class=\"box\" id=\"content\">".htmlspecialchars($_POST['content'])."</textarea></td>";

$form.="<input type=\"textarea\" name=\"content\"value=\"".$_POST["content"]."\">";

echo $form;

EDITED:
spelled htmlspecialchars() correctly... It's important to use some sort of 'screening' for information passed from a text area, and I recommend using it or mysql_real_escape_string() or addslashes() or something on everything you have passed to you via form, especially if you're not 'regular expression scrubbing' everything.

skinsey

10:50 am on Oct 15, 2009 (gmt 0)

10+ Year Member



You are correct about using short form to speed up development. However, when coding you should always use long forms in case the server has the short_open_tag set to 0. By using the long form you can reuse your code on any hosting provider and know it will work.

ecnaralc

10:57 am on Oct 15, 2009 (gmt 0)

10+ Year Member



Okay skinsey everything works except for the echo on the content field. I had to add </textarea> as everything bounced from the bottom of the page into the content area.
So when I do the submit the text disappears and is not echoed.
<td><?php echo "<textarea name=\"content\" cols=\"115\" rows=\"35\" class=\"box\" id=\"content\" value=\"".$_POST["content"]."\">"; ?></textarea></td>

I have not tried TheMadScientist stuff yet.

TheMadScientist

10:58 am on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@skinsey

Fair point...

I use DreamWeaver and TextWrangler, which both have multi-file find and replace options, so it's not too tough to find <?= and replace with <?php echo on a whole site in under 5 mins, so I don't worry about it too much, and if you're are after speed, then you don't want to echo back everything, you want to accumulate and concatenate, which is what I usually use... There's some benchmarking in the library. (The link's @ the top left under the bread-crumbs.) I jump in and out of different styles here quit a bit, usually depending on what I think will be easiest for a person to use, and *obviously* I was off the mark with on this thread...

@ ecnaralc

Yeeeeeeehaw!
Glad you got it.
Mine were just examples for you to look at and possible use depending on what's easier for you... Don't fix it if it's not broken!

skinsey

11:07 am on Oct 15, 2009 (gmt 0)

10+ Year Member



my bad forgot about that bug this should work

<textarea name="content" cols="115" rows="35" class="box" id="content" value="<?php echo &_POST['content'] ?>"</textarea>

This 37 message thread spans 2 pages: 37