defining var

I am working on a form and Have the following html:

<div class="colright"> <h3>Choose # of Words </h3> <P>Pick 1, 2, 3, or 4. More words means more security.</P> <form action="logic.php" method="post"> <label for="length">How Many?&nbsp;</label> <select name="length" id="length"> <option value=2>2</option> <option value=3>3</option> <option value=4>4</option> </select> <div class="form-group checkbox"> <label><br/> <input type="checkbox" name="numbers"> Add numbers? </label> <label><br/> <input type="checkbox" name="symbols"> Add symbols? </label><br /> <!--<label>Choose a Theme: </label><br /> <input type="radio" name="city" value="city" checked>Cities <input type="radio" name="car" value="car">Cars <input type="radio" name="flower" value="flower">Flowers --> <hr> <p><input type="submit"></p> </div> </form> </div>

with the php as follows:

[php]<?php
/* define var and get form data */

$length = $_POST[‘length’];
$numbers = $_POST[‘numbers’];
$symbols = $_POST[‘symbols’];
$pw = array ();
$newPw = array ();
$words = array (“pizza”, “lasagna”, “pasta”, “gelato”);
$ranWords = array_rand($words, 4);

/* generate words for pw generation */

$ranWords;

switch($length){

case $length = 2:
	array_push($pw[$ranWords(0,1)]);
	break;
case $length = 3:
	array_push($pw[$ranWords(0,1,2)]);
	break;
case $length = 4:
	array_push($pw[$ranWords(0,1, 2,3)]);
	break;

}

/* generate some random numbers and symbols */

$someSym = array("!", “@”, “#”, “%”, “&”);
$somenum = array (“1”, “2”, “3”, “4”);
$ranNum = array_rand($somenum, 1);
$ranSym = array_rand($someSym, 1);

If ($numbers = true || $symbols = true){
array_push($pw, $ranNum, $ranSym);
}elseif ($numbers != true || $symbols = true) {
array_push($pw, $ranSym);
}elseif ($numbers = true || $symbols != true) {
array_push($pw, $ranNum);
}else {
$pw = $pw;
}

[/php]

I am getting errors on lines 4, 5, 6 and 19. Not sure why my var’s are not correct.

Appreciate any thoughts…

[php]$length = $_POST[‘length’];[/php]

This says that the variable $_POST[‘length’] exists currently. Which is sort of fine, if the php is on another page, if not you need to use isset() to see if that variable is defined, yet.

You also don’t actually need to move the value from one variable to another. It works in its current state the same way.

Thank you for your thoughts!

The php is a separate file from the form.

So, I have made the changes suggested and now have as PHP:

[php]<?php
/* define var and get form data */

if (isset($_POST[‘length’]) > 0){
$length = $_POST[‘length’];
}else{
$length = 4;
};

if (isset($_POST[‘numbers’])){
$numbers = $_POST[‘numbers’];
};

if (isset($_POST[‘symbols’])){
$symbols = $_POST[‘symbols’];
};

//$length = $_POST[‘length’];
//$numbers = $_POST[‘numbers’];
//$symbols = $_POST[‘symbols’];
//$pw = array();
$newPw = array();
$words = array (“pizza”, “lasagna”, “pasta”, “gelato”);
$ranWords = array_rand($words, 4);
$pw = array()

/* generate words for pw generation
gather desired length of word
*/

switch ($length) {
case $length = 2:
array_push($pw[$ranWords(0,1)]);
break;
case $length = 3:
array_push($pw[$ranWords(0,1,2)]);
break;
case $length = 4:
array_push($pw[$ranWords(0,1,2,3)]);
break;
}

/* generate some random numbers and symbols */

$someSym = array("!", “@”, “#”, “%”, “&”);
$somenum = array (“1”, “2”, “3”, “4”);
$ranNum = array_rand($somenum, 1);
$ranSym = array_rand($someSym, 1);

If ($numbers = true || $symbols = true){
array_push($pw, $ranNum, $ranSym);
}elseif ($numbers != true || $symbols = true) {
array_push($pw, $ranSym);
}elseif ($numbers = true || $symbols != true) {
array_push($pw, $ranNum);
}else {
$pw = $pw;
}

[/php]

The error I see is: Fatal error: Function name must be a string in C:\xampp\htdocs\p2\logic.php on line 31

I’ve reviewed the switch and can’t sort out any mistakes.

Any thoughts?

You are missing a semicolon on line 25.

Thank you. :slight_smile:

I have tweaked the switch command and now get an error near the end.

[php]<?php
/* define var and get form data */

if (isset($_POST[‘length’]) > 0){
$length = $_POST[‘length’];
}else{
$length = 4;
};

if (isset($_POST[‘numbers’])){
$numbers = $_POST[‘numbers’];
};

if (isset($_POST[‘symbols’])){
$symbols = $_POST[‘symbols’];
};

//$length = $_POST[‘length’];
//$numbers = $_POST[‘numbers’];
//$symbols = $_POST[‘symbols’];
//$pw = array();
$newPw = array();
$words = array (“pizza”, “lasagna”, “pasta”, “gelato”);
$ranWords = array_rand($words, 4);
$pw = array();

/* generate words for pw generation
gather desired length of word
*/

switch ($length) {
case 2:
array_push($pw[$ranWords(0,1)]);
break;
case 3:
array_push($pw[$ranWords(0,1,2)]);
break;
case 4:
array_push($pw[$ranWords(0,1,2,3)]);
break;
default:
echo “Oops”;
}

/* generate some random numbers and symbols */

$someSym = array("!", “@”, “#”, “%”, “&”);
$somenum = array (“1”, “2”, “3”, “4”);
$ranNum = array_rand($somenum, 1);
$ranSym = array_rand($someSym, 1);

If ($numbers = true || $symbols = true){
array_push($pw, $ranNum, $ranSym);
}elseif ($numbers != true || $symbols = true) {
array_push($pw, $ranSym);
}elseif ($numbers = true || $symbols != true) {
array_push($pw, $ranNum);
}else {
$pw = $pw;
}
[/php]

The error is: Fatal error: Function name must be a string in C:\xampp\htdocs\p2\logic.php on line 39

IF the preceding lines were ok, why only the last is wrong?

This isn’t jQuery, you don’t need semicolon at the end of closing brackets.

Sponsor our Newsletter | Privacy Policy | Terms of Service