I'm trying to output some mysql data via php INSIDE some javascript (jquery) code. Unfortunately some of the data in my php variable $course_block which goes inside a <SELECT> element contains apostrophes that are anathema to my jquery code and so nothing shows correctly.
My question is pretty basic: how (or can) I get rid of any apostrophe characters coming out of my MySQL database before I even use the data in my JavaScript?
Here's the code I'm using to get the data and to input it into JavaScript (it all works):
$sql = "SELECT title, subject, coursenum, section, crn, instructor, credits, price FROM catalog ORDER BY subject,coursenum";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$subject = $row['subject'];
$coursenum = $row['coursenum'];
$section = $row['section'];
$crn = $row['crn'];
$title = $row['title'];
$instructor = $row['instructor'];
$credits = $row['credits'];
$price = $row['price'];
$course_block .= "<option value=\"$subject-$coursenum-$section-$crn-$title-$instructor-$credits-$price\">$subject $coursenum- $title- Instructor: $instructor- Section: $section- Credits: $credits- Price: \$$price</option>";
}
<script type="text/javascript">
var count = 0;
$(function() {
$('p#add_field').click(function() {
count += 1;
$('#container').append('<select id="field_' + count + '" name="fields[]' + '"> <?php echo "$course_block"; ?> ) </select>');
});
});
</script>
Escaping output data from mysql?
Moderator: General Moderators
Re: Escaping output data from mysql?
I dont think you want to "get rid" of them, but rather just escape them... something as simple as...
$course_block = str_replace("'", "\'", $course_block);
$course_block = str_replace("'", "\'", $course_block);