Apostrophe in Variable

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
letournp
Forum Newbie
Posts: 4
Joined: Wed Jan 05, 2011 4:09 pm

Apostrophe in Variable

Post by letournp »

I have the following code that displays names in with a checkbox. The user selects the names he wants and that then get passed to a variable so that I can post it back to the parrent page. This all works with the exception of names that have appostrophes in them. I have tried using addslashes and stripslashes but have not been able to get what I want.

here is code for the page. any help would be greatly appreciated.

Code: Select all

<?php

if (!isset($_SESSION)) {
  session_start();
}
	//---------------------------------------------------------------
	// input param $selectedList : comma separated selected id/value
	// return $nColumn column check list
	//---------------------------------------------------------------
	function showDynamicCheckList($selectedList, $nColumn) {
		// use single existed connection, make the memory efficient 
		global $CONN;

		// split into array
		$arrSelected = split(",", $selectedList);

		// build option list 
		$sql = "SELECT Rep FROM `Liste Agents Sentiers`";
		$result = mysql_query($sql);

		// column counter
		$colCount = 1;

		$listCheckbox = "<table width='100%'>";
		while($row = mysql_fetch_array($result)) {
			$isSelected = (in_array($row['value'], $arrSelected)) ? "Unchecked" : "";
			
			// first column
			//------------------------------
			if ($colCount == 1) {
				$listCheckbox .= "<tr><td><input type='checkbox' name='myCheckbox[]' value='".$row['Rep']."' ".$isSelected."> ".$row['Rep']."</td>";

				$colCount++;
			
			// last column
			//------------------------------
			} else if ($colCount == $nColumn) {
				$listCheckbox .= "	<td><input type='checkbox' name='myCheckbox[]' value='".$row['Rep']."' ".$isSelected."> ".$row['Rep']."</td></tr>";

				$colCount = 1;

			// other column
			//------------------------------
			} else {
				$listCheckbox .= "	<td><input type='checkbox' name='myCheckbox[]' value='".$row['Rep']."' ".$isSelected."> ".$row['Rep']."</td>";

				$colCount++;
			}
		}				
		$listCheckbox .= "</table>";
		return $listCheckbox;
	}

	// create mysql connection
	$CONN = mysql_connect("localhost", "randonneur_admin", "C4ttAbhT") or die("Could not connect: " . mysql_error());

	// select database used
	mysql_select_db("randonneur_quaddb");

	// hold posted data into variable, build comma separated id
	$selectedList		= "";
	if (isset($_POST['myCheckbox'])) {
		$arrMyCheckbox = $_POST['myCheckbox'];

		for ($i=0; $i<count($arrMyCheckbox); $i++) {
			 $selectedList .= $arrMyCheckbox[$i].", ";
		}
	}

	// print the form
	$_SESSION['SelectedReps'] = $selectedList;
	echo "<form name='fDynamic' method='post'>".
				showDynamicCheckList($selectedList, 3).
				"<input type='submit' value='Selectionner' onclick='post_value();'>".
		"</form>";
	//close connection
	mysql_close($CONN);
?>
<script language="javascript" type="text/javascript">
    var SList = "<?php echo $_SESSION['SelectedReps'] ?>";

    //document.write(SList);
	opener.document.form1.lstPatrouilleurs.value = SList;
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<html>
<style type="text/css">
<!--
.style1 {
	font-size: 24px
}
-->
</style>
<head>
<div align="center">
  <?php 
// Show IF Conditional region1 
if (@$_SESSION['SelectedReps'] != "") {
?>
    <A href="javascript: self.close ()" class="style1">Fermer la fenêtre</A>
    <?php } 
// endif Conditional region1
?> 
    <script langauge="javascript" type="text/javascript">
function post_value(){
self.close();
//}
  </script>
</head>
</div>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Apostrophe in Variable

Post by social_experiment »

letournp wrote:This all works with the exception of names that have appostrophes in them.
So names with apostrophes cannot be displayed or can be displayed but their values is not passed along?

If you have a value in your database that contains an apostrophe (or quotes) use stripslashes() to display them and when you pass the value back to the database use mysql_real_escape_string() to escape them.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
letournp
Forum Newbie
Posts: 4
Joined: Wed Jan 05, 2011 4:09 pm

Re: Apostrophe in Variable

Post by letournp »

I tried using stipslashes but it does not seem to work. the issue is when I pass the data back to the a variable it does not have anything past the apostrophe.

here is what I get when I select the name with the apostrophe and submit the form and do a view source in firefox.

Code: Select all

<form name='fDynamic' method='post'><table width='100%'><tr><td><input type='checkbox' name='myCheckbox[]' value='Alain L'Archevêque' > Alain L'Archevêque</td>...

    var SList = "Alain L, ";   <-- this is where I am having and issue.
Maybe I am just putting the stripslashes in the wrong place. this is where i put it.

Code: Select all

// first column
			//------------------------------
			if ($colCount == 1) {
				$listCheckbox .= "<tr><td><input type='checkbox' name='myCheckbox[]' value='".stripslashes($row['Rep'])."' ".$isSelected."> ".stripslashes($row['Rep'])."</td>";
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Apostrophe in Variable

Post by social_experiment »

Code: Select all

<input type='checkbox' name='myCheckbox[]' value='Alain L'Archevêque' >

The problem is with the ' you are using to contain the value of the input properties. You should use " " instead.

Code: Select all

<input type="checkbox" name="myCheckbox[]" value="Alain L'Archevêque" />
The browser thinks that your value ends after the next ' so that's why it chops off the rest of your value.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
letournp
Forum Newbie
Posts: 4
Joined: Wed Jan 05, 2011 4:09 pm

Re: Apostrophe in Variable

Post by letournp »

Thanks for the reply. This might sound like a dumb questions but how do I do that?

here is the code that I have.

Code: Select all

			if ($colCount == 1) {
				$listCheckbox .= "<tr><td><input type='checkbox' name='myCheckbox[]' value='".stripslashes($row['Rep'])."' ".$isSelected."> ".stripslashes($row['Rep'])."</td>";
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Apostrophe in Variable

Post by Jade »

You change the single quotes to double quotes.

Code: Select all

  if ($colCount == 1) {
                                $listCheckbox .= "<tr><td><input type=\"checkbox\" name=\"myCheckbox[]\" value=\"".stripslashes($row['Rep'])."\" ".$isSelected." /> ".stripslashes($row['Rep'])."</td>";
letournp
Forum Newbie
Posts: 4
Joined: Wed Jan 05, 2011 4:09 pm

Re: Apostrophe in Variable

Post by letournp »

Thank you.
Post Reply