Forum Moderators: coopster
<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>
//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>
<?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"> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center"><input name="save" type="submit" class="box" id="save" value="Save Article"></td>
</tr>
</table>
</form>
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"> </td>
<td> </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 } ?>
elseif(isset($_POST['save']) && (!isset$_POST['section']) ¦¦ empty$_POST['section'])))
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.
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
(['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>
<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>
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.
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.
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>
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!
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.
<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.
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]
<td><textarea name="content" cols="115" rows="35" class="box" id="content"></textarea></td>
<?php echo "<input type=\"textarea\" name=\"content\"value=\"".$_POST["content"]."\">";?>
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.
I have not tried TheMadScientist stuff yet.
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!