Page 2 of 3

Posted: Tue Jun 26, 2007 11:22 am
by knallbernd
Let's say I have chosen one radiobutton from 5 questions. Then this one will be written in the db --> I don't want that.
I only want to write something (or do some other script) when EVERY button is set, no matter the name or what question.

Posted: Tue Jun 26, 2007 11:51 am
by arturm
You can validate in javascript then or:

Code: Select all

$valid = true;
for ($i=1; $i<3; $i++ ) {
    $zahl = "Frage".$i;
    $array[$zahl] = $_POST[$zahl];
    foreach ($array[$zahl] as $key => $wert) {
        if (empty($wert)) {
            $valid = false;
            break;
        }
    }
}

if ($valid) {

// your loops with inserts and update

}
you can put my validation into a function for better look

Posted: Tue Jun 26, 2007 12:05 pm
by knallbernd
Still the same problem...

If only one radiobutton is selected and the others not, this one will be written in the db --> but there should be no
value be written in the db, except if ALL radiobuttons are selected. Javascript is too insecure for this purpose :-(.

Posted: Tue Jun 26, 2007 12:22 pm
by arturm
let me see your code now - my validation should solve the problem

Posted: Tue Jun 26, 2007 12:27 pm
by knallbernd

Code: Select all

$valid = true;
for ($i=1; $i<3; $i++ ) {
    $zahl = "Frage".$i;
    $array[$zahl] = $_POST[$zahl];
    foreach ($array[$zahl] as $key => $wert) {
        if (empty($wert)) {
            $valid = false;
            break;
        }
    }
}

if ($valid) { 

	for ($i=1; $i<3; $i++ ) {
    $zahl = "Frage".$i;
    $array[$zahl] = $_POST[$zahl];
    foreach ($array[$zahl] as $key => $wert) {

		$Name = $key;
		$Abteilung = $y;
        $Wert = $wert;         

	
if ($i==1){
    	$query = "INSERT INTO personen (ID, Name, Abteilung) VALUES (NULL, '$key', '$y')";
    	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
    	
}

    	$aendern = "UPDATE personen SET $zahl = '$wert' WHERE Name = '$key' AND $zahl = ''"; 
    	$ergebnis = mysql_query($aendern) or die('Query failed: ' . mysql_error());
   
    	

echo $ergebnis;


}
}
}
Is this the way you meant it?

Posted: Tue Jun 26, 2007 12:41 pm
by arturm
argh ... my script is checking against $_POST which is wrong
This is obvious that everything that has been POSTed will be set
we should look for values that haven't been POSTed
you can do it by adding all names into the hidden field in the form and then do validation against that values (probably an array)
or you have to use sql query to take all names from the database and then check if they exists in POST

Posted: Tue Jun 26, 2007 12:46 pm
by knallbernd
Thanks, I will try that. If I don't manage to fix it I'm hopefully allowed to post again ;-).
Have a nice day!

Posted: Tue Jun 26, 2007 3:09 pm
by knallbernd
Ok, i don't get it. I'm too silly to create a simple (in this case maybe a bit more difficult) form validation, I think I'm sitting too long behind this script. I just want to validate if every radiobutton ist set. Else the form shouldn't be send.

Code: Select all

<html>
<head>
</head>
<form method="post" action="formular.php">
<?php
error_reporting(E_ALL);



mysql_connect("localhost","root","") or die ("Keine Verbindung moeglich");
mysql_select_db("homepage") or die ("Die Datenbank existiert nicht");

$x = mysql_real_escape_string($_POST["kurs"]);
$abfrage = "SELECT $x FROM lehrende";
$ergebnis = mysql_query($abfrage) or die("MySQL-Fehler: " . mysql_error());

if (mysql_num_rows($ergebnis)) {
  while($row = mysql_fetch_array($ergebnis))
  {           
     $rname = $row[0];
     echo "$rname
  <table>
   <tr><td>Unterrichtsvorbereitung</td>
   <td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"1\"></td>
   <td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"2\"></td>
   <td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"3\"></td>
   <td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"4\"></td>
   <td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"5\"></td>
   <td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"6\"></td>
   </tr>
   
   <tr><td>Fachliche Sicherheit</td>
   <td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"1\"></td>
   <td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"2\"></td>
   <td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"3\"></td>
   <td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"4\"></td>
   <td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"5\"></td>
   <td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"6\"></td>
   </tr>
<br>
   
   </table>";
     
  }
} else {
  echo "No result for {$_POST['kurs']}<br />";
}


?> 
<input type="hidden" name="name" value="<?PHP print ("$x"); ?>">
<input value=" Beurteilung absenden" type="submit">
</form>
</html>

Posted: Wed Jun 27, 2007 9:20 am
by knallbernd
Sorry, me again. I'm now sitting since yesterday and I don't manage do validate my radiobuttons via php. It would be really great if someone could help me out...

Posted: Wed Jun 27, 2007 9:43 am
by CoderGoblin
Try

Code: Select all

<input type=\"radio\" name=\"Frage1[]\" value=\"1\">
with the other options the same. Added empty [] and test the return vals.

To retain values you need to add

Code: Select all

<input type=\"radio\" name=\"Frage1[]\" value=\"1\" checked>

Posted: Wed Jun 27, 2007 9:47 am
by arturm
use what you have already:

Code: Select all

function validateForm() {
  $x = mysql_real_escape_string($_POST["kurs"]);
  $abfrage = "SELECT $x FROM lehrende";
  $ergebnis = mysql_query($abfrage) or die("MySQL-Fehler: " . mysql_error()); 
  
  if (mysql_num_rows($ergebnis)) {
    while($row = mysql_fetch_array($ergebnis)) {           
      $rname = $row[0];

        for ($i=1; $i<3; $i++ ) {
          $zahl = "Frage".$i;
          $array[$zahl] = $_POST[$zahl];
          if (empty($array[$zahl][$rname])) {
            // not all values are set - return false
            return false;
          }
        } 

    }
  }
  // form validated all values set
  return true;
}
make sure you send $kurs in hidden field

Posted: Thu Jun 28, 2007 3:03 am
by knallbernd
Now I'm even more confused...
Do you put that after the echo "rname? And why $kurs in a hidden field - that shouldn't matter to the validation.
Sorry...

Posted: Thu Jun 28, 2007 7:18 am
by arturm
You put my function after you send the form to validate if everything was filled correctly
But you have to send "kurs" to the second page (the best way is to use a hidden field)

And if validate function returns true you can add values to the database
If it returns false you just display error message and "back" button to the form

Posted: Thu Jun 28, 2007 7:37 am
by knallbernd
You mean that I put this function into "formular.php" (the site the form is send to)? But why du you SELECT everything again?
Maybe you could paste the into the existing code because I don't understand it right now :(

Posted: Thu Jun 28, 2007 7:43 am
by arturm
You select everything again because you want to compare if values that have been sent match values that were in the form.
This is the best way to check if everything was selected.