0

Hi, I am generating a table for use as a revision form. The following line:

printf("<td><input type='text' name='%s' onfocus='showDisplayName($v[1], $v[2]);' 
           		id='%s' value='%s'></input></td></tr>\n", strtolower($key), strtolower($key), $value);

has the php function showDisplayName() embedded in it. This function will not work. If I write the identical line in a pure HTML page it works fine.

Attachments: reviseTbl.png Generated output table
generatedHTML.png Generated code
ContactRevise.php Source code

Would appreciate any help in making the changes so it works.

Thanks in advance, RP

Attachments
<?php session_start();

set_include_path( '../include' );
error_reporting (E_ALL ^ E_NOTICE);

require_once "functions.inc";
 
$loc=$_GET['locFlg'];
 
if($loc=='insert') {
	$revId=$_SESSION['lastId'];
	}else {
		$revId=$_SESSION['data[id]'];
	};
	
$k=array();
$v=array();
 
if(!$revId) $revId=$_GET['id']; 

   $hostName = "localhost";
   $databaseName = "contacts";
   $username = "rick";
   $password = "rick";
   $table="cdata";
   
//  ===================  function contains()  =============
 
 function contains($haystack, $needle) {
 	$haystack=strtolower($haystack);
 	$needle=strtolower($needle);
	if(strpos($haystack, $needle)>0) return true; 	
 	}
 	
//  ===================  showDisplayname()  =============	

 function showDisplayName ($fName, $lName) {
	
	return $lName .", ".$fName; 	
 	} 	
 	
//  ===================================================== 
  	
   	$cxn = @ mysqli_connect($hostName, $username, $password, $databaseName); //Connect to the MySQL server
   
     if (mysqli_connect_errno()) {
    		printf("Connect failed: %s\n", mysqli_connect_error());
    		exit();
	};
	
	$query=sprintf("SELECT * FROM %s WHERE Name_Id=%s;", $table, $revId);

//echo $query;

	$result = mysqli_query($cxn, $query) or exit( mysqli_error( $cxn ));
	
		        while($row = mysqli_fetch_assoc($result)) {

	        $rows[]=$row;
		    }
	
foreach($rows[0] as $key => $value) {
	if($value!="") $connectData[$key]=$value;
}

printf("<div id='who'>");
printf("<fieldset class='rev1'>\n");
printf("<legend>Revising Record %s </legend>\n", $revId);
printf("<form name='revContactForm' id='revContactForm' method='POST' action=''>\n");
printf("<table class='revForm' border='3' cellpadding='5'  bgcolor='#feede3' ><tr>");



foreach($connectData as $key => $value) {
$k[]=$key;
$v[]=$value;
		
	if($key=='Name_ID') {
		continue;
	};
		printf("<td><label for='%s'>%s</label></td>\n",strtolower($key), preg_replace("/_/"," ",$key));
		
		if($key=="Display_Name") {
			
           printf("<td><input type='text' name='%s' onfocus='showDisplayName($v[1], $v[2]);' 
           		id='%s' value='%s'></input></td></tr>\n", strtolower($key), strtolower($key), $value);
			continue;
		};		
		
		
		if (contains($key, "Notes")) {
			printf("<td><textarea name='%s' id='%s'  rows=5 cols=81> '%s' </textarea></td></tr>\n",strtolower($key),
			 strtolower($key), $value);
			continue;
		};
	
		printf("<td><input type='text' name='%s' id='%s' value='%s'></input></td></tr>\n", strtolower($key), strtolower($key), $value);
		
	}; // if key	

//printf("<tr><td><input type='submit' name='submit1' id='submit1'>Revise Record</input></td></tr>\n");
printf("<tr><td><input type='submit' class='hov'  name='submit1' value='Save Contact Data'></input></td></tr>\n");	
printf("</table></form></fieldset></div>\n");

//listA("key", $k);

//listA("value", $v);
/*
printf("</div> <!-- inputForm -->\n");
     
  
 printf("<div id='controls'>\n");
 
	 printf("<button type='button' class='hov' name='connect onclick=\"window.location.href='ContactFrontEnd.php'\">Go Back</button>");            
 	printf("<input type='submit' class='hov'  name='operation' value='Save Contact Data'>");
 printf("</div>\n");
 */
 			

      
?>
generatedHTML.png 138.22 KB reviseTbl.png 45.89 KB
2
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by pizzipie
0

If you mean:

...onfocus='showDisplayName(\'$v[1]\', \'$v[2]\');'
id='%s'...

it doesn't work. This is generated code:

...name="display_name" onfocus="showDisplayName(\" frank="" &="" doady\',="" \'paddon1009\');'="" id="display_name"...


Can you show me code please?

Thanks, RP

0
printf(
  '<td><input type="text" name="%s" onfocus="showDisplayName(\'%s\', \'%s\');" id="%s" value="%s"></input></td></tr>\n', 
  strtolower($key), $v[1], $v[2], strtolower($key), $value);

If there are quotes in $v they might be messing it up.

Edited by pritaeas: n/a

0

Thanks again,

After looking at the results of incorporating your change to my code I realize I am making a major logic error.

printf(
    '<td><input type="text" name="%s" onfocus="showDisplayName(\'%s\', \'%s\');" id="%s" value="%s"></input></td></tr>\n',
    strtolower($key), $v[1], $v[2], strtolower($key), $value);

in place of \'%s\', \'%s\' I need to put something like $(#last_name.val())+", "+$(#first_name.val()) which means I have to mingle jquery with PHP. What my original code does not do is insert the changed values of first_name and last_name to create display_name.

Is this possible??

RP

0

You are using jQuery. I would leave out the parameters for showDisplayName(), and just get them in that function. Is that what you mean ?

0

:):) SOLVED;

I have used this function in another application.
Can it somehow be called from within my PHP program? I just answered my own question.

I put the following function in printf() statements and it works just fine.

Thanks for your time in helping me.

RP,

<script type="text/javascript" >

           function showDisplayName() {
     	       	var first = document.getElementById('fname');
    		var second = document.getElementById('lname');
    		 var third = document.getElementById('dname');
		third.value=second.value+", "+first.value;
	}

</script>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.