phpacademy Forum

Watch, Listen, Learn!

Form fields with same name value

For if you require help with PHP programming.

Form fields with same name value

Postby bullbreed » Sun Feb 07, 2010 3:36 pm

HI Guys

I am writing a News Article section and I have 2 form fields with the same value called article_section.

The reas9on I have 2 is because if you write an article you can create an article section by typing it in to a form text field or select an existing section from an article drop down menu form field.
I cant seem to et it working so I am assuming I cant call the form elements the same ame (article_section).

Heres the code

[php]<?php
$submit = $_POST['submit'];

//Form data
$articlesection = mysql_real_escape_string($_POST['article_section']);
$articletitle = mysql_real_escape_string($_POST['article_title']);
$articleptitle = mysql_real_escape_string($_POST['article_ptitle']);
$articlepdescription = mysql_real_escape_string($_POST['article_pdescription']);
$articlepkeywords = mysql_real_escape_string($_POST['article_pkeywords']);
$articlecontent = mysql_real_escape_string($_POST['article_content']);
$date = date('d/m/Y \a\t g:i.s a');

if ($submit){

//Open the database
mysql_connect("localhost","root","");
mysql_select_db("*************"); //Select the database

$pagecheck = mysql_query("SELECT `article_title` FROM articles WHERE `article_title` = '$articletitle'");echo mysql_error();
$count = mysql_num_rows($pagecheck);echo mysql_error();

if ($count != 0){
echo("<font size=\"2\" color=\"#ff0000\">Article already exists. Please edit the existing article!</font>");echo mysql_error();
}else{

//Check for existing fields
if ($articlesection && $articletitle && $articleptitle && $articlepdescription && articlepkeywords && $articlecontent && $date){

//Enter into Database
$queryreg = mysql_query("INSERT INTO articles (`article_section`, `article_title`, `article_ptitle`, `article_pdescription`, `article_pkeywords`, `article_content`, `date`) VALUES ('$articlesection', '$articletitle', '$articleptitle', '$articlepdescription', '$articlepkeywords', '$articlecontent', '$date')");
echo("<font size=\"2\" color=\"#00cc00\">Your article has been created! </font>");
}else{
echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>");
}
}
}
?> [/php]

Heres the form

[php]<form action="addarticle.php" method="post" enctype="multipart/form-data" name="addarticle" target="_self" id="addarticle">
<!-- Form Starts -->
<div class="titlearea">Add News Article</div>
<div class="dataarea">
<div class="infowrap">Article title</div>
<div class="infowrap">Create Article Section</div>
<div class="infowrap">Select Article Section</div>
<div class="infowrap"><input name="article_title" type="text" id="article_title" size="25" maxlength="20" />
</div>
<div class="infowrap"><input name="article_section" type="text" id="article_section" size="25" maxlength="100" />
</div>
<div class="infowrap">
<label></label>
<label>
<select name="article_section" id="article_section">
<option><?php echo $row["article_section"]; ?></option>
</select>
</label>
</div>
</div>

<div class="titlearea">Meta Data</div>
<div class="dataarea">
<div class="metawrap">Title - Please use approx 65 characters</div>
<div class="metawrap"><input name="article_ptitle" type="text" id="article_ptitle" size="80" maxlength="20" />
</div>
<div class="metawrap">Description - Please use approx 155 characters inc spaces</div>
<div class="metawrap"><input name="article_pdescription" type="text" id="article_pdescription" size="80" maxlength="200" />
</div>
<div class="metawrap">Keywords - Please use approx 5 keyword per page</div>
<div class="metawrap"><input name="article_pkeywords" type="text" id="article_pkeywords" size="80" maxlength="200" />
</div>
</div>

<div class="titlearea">Article Content</div>
<div class="dataarea">
<textarea name="article_content" cols="80" rows="20" id="article_content"></textarea>
</div>
<div class="dataarea">
<input type="submit" value="Save" name="submit" /></div>
</form>[/php]

Is this the right way to do it?
bullbreed
 
Posts: 244
Joined: Sat Jan 02, 2010 10:48 pm
Online: 3h 33m 41s
Karma: 0

Re: Form fields with same name value

Advertisment

Advertisment
 

Re: Form fields with same name value

Postby Cags » Sun Feb 07, 2010 3:42 pm

I'm not sure I fully understand your requirement, but no you can't have two elements with the same name without encountering problems. Just give it another name. Then you can simply check if the first ones empty, if it is check the value of the second.
"I don't suffer from insanity, I enjoy every minute of it."
- Pete
CodeCanyon - Cheap, High Quality, Ready Made Scripts.
User avatar
Cags
Moderator
 
Posts: 1852
Joined: Fri May 22, 2009 9:35 am
Location: Purgatory
Online: 2d 22h 26m 42s
Karma: 9

Re: Form fields with same name value

Postby bullbreed » Sun Feb 07, 2010 3:58 pm

OK thanks. Just not quite sure how to write it.
bullbreed
 
Posts: 244
Joined: Sat Jan 02, 2010 10:48 pm
Online: 3h 33m 41s
Karma: 0

Re: Form fields with same name value

Postby bullbreed » Sun Feb 07, 2010 4:33 pm

How do I check them and also how do I echo all the info in the article_section column in to a Menu form Field

1. Checking the form fields exist; I added an extra variable called $articleesection (added the extra e)
Do I do...

[php]
if (!isset($articlesection)) {

//Check for other fields
if ($articleesection && $articletitle && $articleptitle && $articlepdescription && articlepkeywords && $articlecontent && $date){

//Enter into Database
$queryreg = mysql_query("INSERT INTO articles (`article_esection`, `article_title`, `article_ptitle`, `article_pdescription`, `article_pkeywords`, `article_content`, `date`) VALUES ('$articlesection', '$articletitle', '$articleptitle', '$articlepdescription', '$articlepkeywords', '$articlecontent', '$date')");
echo("<font size=\"2\" color=\"#00cc00\">Your article has been created! </font>");
}
[/php]

The add another if statement saying

if (isset(articlesection)

enter this iinstead.

Sorry guys just not to sure
bullbreed
 
Posts: 244
Joined: Sat Jan 02, 2010 10:48 pm
Online: 3h 33m 41s
Karma: 0

Re: Form fields with same name value

Postby bullbreed » Sun Feb 07, 2010 10:26 pm

Could someone point me in the right direction please :)
bullbreed
 
Posts: 244
Joined: Sat Jan 02, 2010 10:48 pm
Online: 3h 33m 41s
Karma: 0

Re: Form fields with same name value

Postby bullbreed » Mon Feb 08, 2010 2:56 pm

Anyone :)

Im not quite sure how to check in the category_section is completed and if so do somethng or if not check another form field has something in it
bullbreed
 
Posts: 244
Joined: Sat Jan 02, 2010 10:48 pm
Online: 3h 33m 41s
Karma: 0

Re: Form fields with same name value

Postby libeco » Mon Feb 08, 2010 5:19 pm

I think this can be quite tricky as a user possible changes the dropdown by accident and you end up with a problem. Perhaps it's better to split the forum into parts to first check if you use the dropdown or the textfield. Another option is a JavaScript solution where users push a button to show the textfield instead of the dropdown.
Common PHP/MySQL errors | How to display code on the forum
"It always seems impossible until its done." - Nelson Mandela
User avatar
libeco
Moderator
 
Posts: 2466
Joined: Fri Apr 24, 2009 9:03 pm
Location: Netherlands
Online: 5d 9h 32m 31s
Karma: 25

Re: Form fields with same name value

Postby bullbreed » Mon Feb 08, 2010 9:16 pm

Ok so what if I did it a bit differently.

What if I made 2 form elements
exist_cat - This would be a drop down menu form field with a value of 'None'
new_cat - This would be an input text field which would have no value

Then I could say

If exist_cat doesn't = 'None'
Insert exist_cat in to the database
If exits_cat does = 'None'
Insert into new_cat in to the database

Then check the rest of the form and insert that in to the database

I had a go at writing the script. Could someone point out if it will work and what's wrong with the code

[php]<?php
$submit = $_POST['submit'];

//Form data
$existcat = mysql_real_escape_string($_POST['exist_cat']);
$newcat = mysql_real_escape_string($_POST['new_cat']);
$articletitle = mysql_real_escape_string($_POST['article_title']);
$articleptitle = mysql_real_escape_string($_POST['article_ptitle']);
$articlepdescription = mysql_real_escape_string($_POST['article_pdescription']);
$articlepkeywords = mysql_real_escape_string($_POST['article_pkeywords']);
$articlecontent = mysql_real_escape_string($_POST['article_content']);
$date = date('d/m/Y \a\t g:i.s a');

if ($submit){

//Open the database
mysql_connect("localhost","root","");
mysql_select_db("********"); //Select the database

if ($existcat !== 'None'){
$queryreg = mysql_query("INSERT INTO articles (`article_cat` VALUES '$existcat')");
}else{
if ($existcat == 'None'){
$queryreg = mysql_query("INSERT INTO articles (`article_cat` VALUES '$newcat')");
}
}

//Check for existing fields
if ($articletitle && $articleptitle && $articlepdescription && articlepkeywords && $articlecontent && $date){


$pagecheck = mysql_query("SELECT `article_title` FROM articles WHERE `article_title` = '$articletitle'");echo mysql_error();
$count = mysql_num_rows($pagecheck);echo mysql_error();

if ($count != 0){
echo("<font size=\"2\" color=\"#ff0000\">Article already exists. Please edit the existing article!</font>");echo mysql_error();
}else{

//Enter into Database
$queryreg = mysql_query("INSERT INTO articles (`article_title`, `article_ptitle`, `article_pdescription`, `article_pkeywords`, `article_content`, `date`) VALUES ('$articletitle', '$articleptitle', '$articlepdescription', '$articlepkeywords', '$articlecontent', '$date')");
echo("<font size=\"2\" color=\"#00cc00\">Your article has been created! </font>");
}else{
echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>");
}
} //end Check for existing fields
}//end if submit


?>[/php]

Thanks
bullbreed
 
Posts: 244
Joined: Sat Jan 02, 2010 10:48 pm
Online: 3h 33m 41s
Karma: 0

Re: Form fields with same name value

Postby libeco » Mon Feb 08, 2010 9:37 pm

No that wouldn't work as you would be storing the same data in different columns. Why don't you try to make a two step form? You could simply enter the $_POST data into the new form on the second page, which wouldn't allow editing those values.
Common PHP/MySQL errors | How to display code on the forum
"It always seems impossible until its done." - Nelson Mandela
User avatar
libeco
Moderator
 
Posts: 2466
Joined: Fri Apr 24, 2009 9:03 pm
Location: Netherlands
Online: 5d 9h 32m 31s
Karma: 25

Re: Form fields with same name value

Postby bullbreed » Mon Feb 08, 2010 10:16 pm

I'm not sure I need 2 pages just to add a category.

Also I didn't think I was storing the same data in different columns. What I was trying to do was say

If the value of exist_cat drop down menu doesn't = None;
Then they must have selected something because the default value = None;
So insert this in to the article_section column;
else
If the value of exist_cat drop down menu does = 'None';
They haven't selected anything
So insert the value of new_cat input text field into the article_section column
bullbreed
 
Posts: 244
Joined: Sat Jan 02, 2010 10:48 pm
Online: 3h 33m 41s
Karma: 0

Next

Return to PHP Help

Who's online?

Users browsing this forum: No registered users and 1 guest