single quotes in data break the page

single quotes in data break the page

I have a php page that populates a form from data pulled from a postgresql database. Jquery is used to populate the form with the pulled data as below. If any piece if data has a single quote in it, like Jim's the the form is displayed but no data is populated in any field.
  1. $(document).ready(function(){
            <?
                $i=0;
                foreach($keys as $key){
                   
                    if ($i%2==1){
                        if($key!="reason" && $key!="parent_deceased" && $key!="to_account" && $key!="dispersed"){
                            echo("$('input[name=\"".$key."\"]').attr('value','".$queryResults[$key]."');\n");
                        }
                    }//end if
                    $i++;
                }










  2. ...
  3. <div class="textField left">
                                    First Name:
                                </div>
                                <div class="inputField left">
                                    <input type="text" class="inputbox req-string" name="studentfname">   
                                </div>
                                <div class="textField left">
                                    Last Name:
                                </div>
                                <div class"inputField">
                                    <input type="text" class="inputbox req-string" name="studentlname">
                                </div>