Bootstrap dynamically set modal title in phpAsk Question

问题:

I've been trying to change my modal title remotely from php. The title that I want to be set is a variable and has already been assigned but it changes when the user inputs something new.

Here is my php code:

$Studname = $results['Studentname'];
echo '<script type="text/javascript">';                
echo 'alert("'.$Msg.$Studname.'")';
echo '</script>';

echo "<script type='text/javascript'>
    $(document).ready(function(){
        $('#MyAdvertLabel').text(".$Studname.");
    });
</script>";

echo "<script type='text/javascript'>
    $(document).ready(function(){
        $('#myAdvert').modal('show');
    });
</script>";

There seems to be no error in the code according to the program i'm using but still when I run the code there is no change in the modal title.

回答1:


Your PHP will generate a Javascript error:

<?php
$Studname = 'Sample text';
$('#MyAdvertLabel').text(".$Studname.");
?>

Will generate:

$('#MyAdvertLabel').text(sample text);

The contents of the text() function need to be wrapped in quotation marks.

For example:

<?php
$Studname = 'Sample text';
$('#MyAdvertLabel').text('".$Studname."'); // Notice the ' ' wrapping the variable
?>
标签: php jquery twitter-bootstrap
© 2014 TuiCode, Inc.