Intergrating JavaScript and PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Intergrating JavaScript and PHP?

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Last edited by s.dot on Sat Dec 10, 2005 3:58 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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:
Post Reply