PDA

View Full Version : Form & Arrays not working for me


ntburchf
01-14-2003, 09:54 AM
Almost got it but,
fairly new at this, so I can not get my form to pass the array correctly.
What it is doing is passing the array as either [ARRAY], or appending arrayAdmin to the variable that I am trying to pass.

It takes the first pick list and posts it as: (this is how it shows up in database)

arrayArguing with Admin

so even though I pick say 7 admins, it only displays the first pick but it appends that array word to it.

The other pick lists do the same, appends the word array to the choice, and only 1 choice is posted to the database.

Form Entry snipet


<form enctype='multipart/form-data' action='process_1.php' method='post'>

<select name="Offense[]" class="textfield" multiple size="5">
<option value='Arguing with Admin'>Arguing with Admin (AA)</option>
<option value='Bug Exploit'>Bug Exploit (BE)</option>

<select name="Admin[]" class="textfield" multiple>
<option value='Acedel'>Acedeal</option>
<option value='Coby_Wan_Kenobi'>Coby_Wan_Kenobi</option>
<option value='Bush_8'>Bush_8</option>

<input type=submit value='Send' >



<?php

include("admin/config.inc.php");

$errors=0;
$error="The following errors occured while processing your form input.<ul>";
$_POST['Notes']=preg_replace("/(\015\012)|(\015)|(\012)/","&nbsp;<br />",
$_POST['Notes']);
if
($_POST['Username']=="" || $_POST['Offense']=="" || $_POST['Admin']=="" || $_POST['Date']=="" || $_POST['Time']==""
|| $_POST['Server']=="" || $_POST['Notes']=="" )
{

$errors=1;
$error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
}
if($errors==1) echo $error;
else
{

$Offense = $_POST["Offense"];
if (is_array($Offense))
{
for ($i=0; $i<count($Offense); $i++) {
$Offense .= $Offense[$i].", ";
}
$Offense = substr ($Offense, 0, strlen ($Offense)-2);
}
else
{
$Offense = "";
}

$Admin = $_POST["Admin"];
if (is_array($Admin)) {
for ($i=0; $i<count($Admin); $i++)
{
$Admin .= $Admin[$i].", ";
}
$Admin = substr ($Admin, 0, strlen ($Admin)-2);
}
else
{
$Admin = "";
}

$Server = $_POST["Server"];
if (is_array($Server))
{
for ($i=0; $i<count($Server); $i++)
{
$Server .= $Server[$i].", ";
}
$Server = substr ($Server, 0, strlen ($Server)-2);
}
else
{
$Server = "";
}


//change $_POST to $_GET or $_SESSION or $ENV, etc. whichever array you need to view.
//echo "Values submitted via POST method:<br>";
//reset ($_POST);
//while (list ($key, $val) = each ($_POST)) {
// echo "$key => $val<br>";
// }


$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$_SERVER['HTTP_HOST'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
$message="Username: ".$_POST['Username']."
Offense: $Offense
Admin: $Admin
Date: ".$_POST['Date']."
Time: ".$_POST['Time']."
Server: $Server
Notes: ".$_POST['Notes']."
";


$link = mysql_connect($hostname,$username, $password) or die("Connetion to database failed!");
mysql_select_db($dbname);

$query="insert into server_user_banlist (usernname,offense,admin,date,time,server,notes) values ('".$_POST['Username']."','$Offense','$Admin','".$_POST['Date']."','".$_POST['Time']."',
'$Server','".$_POST['Notes']."')";
mysql_query($query);
header("Refresh: 0;url=form8.html");
}
?>

I've tried numberous things, but not really sure where to put some of the code that I have found, given as advice etc.
Was hoping one of you gurus here could help

What am I doing wrong here and how can I condense this/?
Thanks in advance
TB.

Ökii
01-14-2003, 11:15 AM
naming the selects with offence[] designates an array named offence. So...

$Offense = $_POST["Offense"];

doesn't make any sense as $_POST['Offense'] would just return the word array.

try..

for each($ofens as $_POST["Offense"])
{
// loop here
echo $ofens.'<br />';
}

or something similar to iterate through the $Offense array.

ntburchf
01-14-2003, 09:27 PM
So trying to follow the logic here;
Not sure doing correct, first attempt at array(s) and changing the values of them.



foreach($Offense as $Rules) {
//tells php to assign the value of the array at each position to the //new variable $Rules .
$Rules .= $Offense . ", "; //then cancatenates the variable $var to the end of the variable $new_var
Then simply close the foreach.
}

I then should be able to take the new array value(s) $Offense and get them into the database. ??

Ökii
01-15-2003, 11:22 AM
Sort of...

The array $Offense would still be an array inside the foreach loop. You should only be working with the $Rules variable there and ignore the $Offense bit.

foreach($Offense as $Rules)
{
echo $Rules.'<br />';
}

Well done on inverting the parameters from my last example btw.

Anyway - the above would iterate through the array $Offense and attribute the value of each index to $Rules (as a var=val rather than var=array)

So, perhaps

$offense_string = '';
foreach($_POST['Offense'] as $Rules)
{
$offense_string .= $Rules.', ';
}
$textual_offense = substr($offense_string,0,-2);

ntburchf
01-16-2003, 05:04 AM
Thanks A million Ökii
Got it working now, with a little more understanding of arrays.
Once again thanks..

Now if I could just get the date file to go in correct :)
Regards
TB:thumbsup: