Jump to content

Php & Ajax Character Encoding


rakelbara

Recommended Posts

Hi! I have problem with character encoding in an php/ajax dropdown menu. I found a tutorial that I have been working on to fit my needs but I get a problem with the character encoding. The tutorial is here: http://www.coursesweb.net/ajax/multiple-select-dropdown-list-ajax_t My example is here: http://www.hafdal.dk/testing/test.php I have added header('Content-type: text/html; charset=utf-8'); to both php files and the tables in mysql are all utf8_general_ci. But nothing helps :fool:Any ideas would be GREATLY appreciated!Thanks!

Link to comment
Share on other sites

Use utf8_encode on the text that goes out. It needs to actually be UTF8 text, you can't just claim that it is. You'll also probably want to actually save the files as UTF8 files. You can use Sublime Text in my signature if you don't have a text editor that can do that.

Link to comment
Share on other sites

Ok I'm not quite sure what you mean by "Use utf8_encode on the text that goes out". But I just took all three files, copied the text into new files and saved them with the UTF8 format - just to be absolutely sure. Unfortunately it did not help :-/

Link to comment
Share on other sites

YAY I managed to find a solution:I added this to my test.php file (apparently the header I had added was not enough) <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 /> So now my select list looks correct BUT somehow ajax does not interpret the value in the dropdown menu as it should. My guess is that it is still the encoding that is incorrect somehow. I choose a value in the dropdown list and get no second dropdown menu. Any ideas?? I forgot to thank you in my last post so THANK YOU for taking the time to look at this with me :-)

Link to comment
Share on other sites

Well it should pass the value on to the next dropdown menu - but it doesn't. A selection in the first dropdown should result in another dropdown, but somehow the value that is in the dropdown menu is not "good enough"/does not correspond with a value that is needed to generate the second dropdown.

Link to comment
Share on other sites

I'm beginning to think it's the php file - my guess (and it is a guess) is this:

// sets the "onchange" event, which is added in <select> tag  $onchg = $next_col!==null ? " onchange=\"ajaxReq('$next_col', this.value);\"" : '';

But I guess that's also input from the ajax file....? Here's all of the code:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 /><?php // Multiple select lists - www.coursesweb.net/ajax/if(!isset($_SESSION)) session_start(); // Here add your own data for connecting to MySQL database$server = 'xxxxxxxxxxx';$user = 'xxxxxxxxxx';$pass = 'xxxxxxxxxxx';$dbase = 'xxxxxxxxxxxxxx'; // Here add the name of the table and columns that will be used for select lists, in their order// Add null for 'links' if you don't want to display their data too $table = 'view_syslurhrepparbæir';$ar_cols = array('sysla', 'hreppur', 'null', 'links');$preid = 'slo_';		// a prefix used for element's ID, in which Ajax will add <select>$col = $ar_cols[0];	 // the variable used for the column that wil be selected$re_html = '';		  // will store the returned html code // if there is data sent via POST, with index 'col' and 'wval'if(isset($_POST['col']) && isset($_POST['wval'])) {   // set the $col that will be selected and the value for WHERE (delete tags and external spaces in $_POST)  $col = trim(strip_tags($_POST['col']));  $wval = "'".trim(strip_tags($_POST['wval']))."'";}  $key = array_search($col, $ar_cols);			// get the key associated with the value of $col in $ar_cols$wcol = $key===0 ? $col : $ar_cols[$key-1];	 // gets the column for the WHERE clause$_SESSION['ar_cols'][$wcol] = isset($wval) ? $wval : $wcol;	// store in SESSION the column and its value for WHERE // gets the next element in $ar_cols (needed in the onchange() function in <select> tag)$last_key = count($ar_cols)-1;$next_col = $key<$last_key ? $ar_cols[$key+1] : '';$conn = new mysqli($server, $user, $pass, $dbase);	 // connect to the MySQL databaseif (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); }	 // check connection // sets an array with data of the WHERE condition (column=value) for SELECT queryfor($i=1; $i<=$key; $i++) {  $ar_where[] = '`'.$ar_cols[$i-1].'`='.$_SESSION['ar_cols'][$ar_cols[$i-1]];} // define a string with the WHERE condition, and then the SELECT query$where = isset($ar_where) ? ' WHERE '. implode($ar_where, ' AND ') : '';$sql = "SELECT DISTINCT `$col` FROM `$table`".$where;$result = $conn->query($sql);	   // perform the query and store the result // if the $result contains at least one rowif ($result->num_rows > 0) {// sets the "onchange" event, which is added in <select> tag  $onchg = $next_col!==null ? " onchange=\"ajaxReq('$next_col', this.value);\"" : ''; // sets the select tag list (and the first <option>), if it's not the last column  if($col!=$ar_cols[$last_key]) $re_html = $col. ': <select name="'. $col. '"'. $onchg. '><option>- - -</option>';  while($row = $result->fetch_assoc()) {  // if its the last column, reurns its data, else, adds data in OPTION tags	if($col==$ar_cols[$last_key]) $re_html .= '<br/>'. $row[$col];	else $re_html .= '<option value="'. $row[$col]. '">'. $row[$col]. '</option>';  }  if($col!=$ar_cols[$last_key]) $re_html .= '</select> ';		// ends the Select list}else { $re_html = '0 results'; }$conn->close();// if the selected column, $col, is the first column in $ar_colsif($col==$ar_cols[0]) {  // adds html code with SPAN (or DIV for last item) where Ajax will add the select dropdown lists  // with ID in each SPAN, according to the columns added in $ar_cols  for($i=1; $i<count($ar_cols); $i++) {	if($ar_cols[$i]===null) continue;	if($i==$last_key) $re_html .= '<div id="'. $preid.$ar_cols[$i]. '"> </div>';	else $re_html .= '<span id="'. $preid.$ar_cols[$i]. '"> </span>';  }  // adds the columns in JS (used in removeLists() to remove the next displayed lists when makes other selects)  $re_html .= '<script type="text/javascript">var ar_cols = '.json_encode($ar_cols).'; var preid = "'. $preid. '";</script>';}else echo $re_html;?>

Link to comment
Share on other sites

No scratch that - I'm pretty sure its the script call in the php file:

// adds the columns in JS (used in removeLists() to remove the next displayed lists when makes other selects)  $re_html .= '<script type="text/javascript">var ar_cols = '.json_encode($ar_cols).'; var preid = "'. $preid. '";</script>';

Link to comment
Share on other sites

What makes you think that's the problem? Add some error reporting code to that PHP script to make sure that all errors get logged. Create a file in the same directory as the PHP file called "error.log", and make sure that it has write access for PHP. If you're uploading these files to your server using an FTP client, that means right-clicking on the file, selecting Permissions, and making sure the Write box is checked. Once that's there, add this to the top of your PHP code:

ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('display_errors', 0);error_reporting(E_ALL);

Run the code again and then check the error log to see what's there. You can also just load the PHP file directly in the browser rather than accessing it through ajax. That will cover PHP errors, for Javascript errors you'll need to check the error console in your browser.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...