Page 1 of 1

Intergrating JavaScript and PHP?

Posted: Sat Dec 10, 2005 3:46 pm
by LiveFree
Hello,

Suppose I have this forum validation:

Code: Select all

if(isset($_POST['submitted'])){
  if (!empty($_POST['lname'])){
    $lname=trim($_POST['lname']);
  }else{
   error[]="Please enter your lastname";
  }
  if (!empty($_POST['rank'])){
    $rank=$_POST['rank'];
  }else{
    error[]="-Please select your rank";
  }
  if (!empty($_POST['email'])){
    $e=trim($_POST['email']);
  }else{
    error[]="-Please enter your email";
  }
  if (!empty($_POST['xfire'])){
    $xfire=trim($_POST['xfire']);
  }else{
    error[]=-Please enter your Xfire ID";
  }
  if (!empty($_POST['squad'])){
    $squad=$_POST['squad'];
  }else{
    error[]="-Please select your squad";
  }
And instead of the echo statements, I want to have JavaScrtipt's alert() function to pop up a box that says the warning, but every warning generated from the arrays would be in one alert box

How would I go about doing this?

Posted: Sat Dec 10, 2005 3:50 pm
by s.dot
#1 error[] should be $error[]

#2 don't put it into an array, put it into a string.. like this

Code: Select all

$error = ""; // empty string

// when there is an error do this
$error .= "error message 1\n"; // add error message to error string

// another error
$error .= "error message 2\n"; // add second error message to error string
to print out your error in an alert box

Code: Select all

echo "<script type=\"text/javascript\">alert('$error');</script>"; // this will display both errors

Posted: Sat Dec 10, 2005 3:52 pm
by shiznatix
1st, you have a parse error in your php. 2nd, this belongs in the client side forum. 3rd, your answer :)

Code: Select all

<script language="JavaScript" type="text/JavaScript">
<!--
function formCheck(formobj){

	// Enter name of mandatory fields, has to be the same as their name in the form
	var fieldRequired = Array("firstname", "lastname", "address");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("First Name", "Last Name", "Address");
	
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			//return ValidateForm();
			case "select-one":			
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";					
				}
				break;
			case "text":  			
			case "password":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;		
			
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}		
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
	
	
}
// -->
</script>
then do

Code: Select all

<form name="myform" action="page.php" method="post" onsubmit="return formCheck(this)">
edit: seams i was a bit slow, but I like my solution better :lol: