Bootstrap dynamically set modal title in php - javascript

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.

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
?>

Related

How to refresh a specific div content along with the link id when clicked on a link?

I am trying to refresh the div content by clicking on href link along with the id(named as data), my actual problem is I'm unable to get the id value with in the script because it is passed with in the function. How to pass the data variable in my php to script? Please check my code below and help me out to solve this. img_div is id of my div which is to be refreshed
PHP:
function printSubDir($dir, $path){
global $data;
$data = $path.''.$dir;
echo "<li><span class=\"toggle\"><a href='asset1.php?data=$data' id='refresh'>$dir</a></span>";
createDir($path.$dir."/");
echo "</li>";
}
Script:
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#refresh').click(function(){
$('#img_div').load('asset1.php #img_div', function() {
});
});
});
</script>
Hi assuming that your variable is $data.You can a php variable in javascript like the following
<?php
$data = "Value of your variable";
?>
<script>
// now a javascript variable data stores the value of your php variable
var data= "<?php echo $data?>";
</script>
I Hope this heps
Echo the id into the function call:
<?php
echo "<a onClick='handleClick($id)'>$dir</a>";
?>

phpcoding special characters

When I load a php page, i put within a javascript function, a name. The problem comes when this string has special chars like '.
Here I paste the code of a click event:
showSocialShare(event, '<?php echo $object->slug; ?>', '<?php echo htmlspecialchars($object->title); ?>', '<?php echo $object->image; ?>')
I thought that the function htmlspecialchars code somehow the string but the result is:
showSocialShare(event, '4049269', 'collection-'Noun'', '/img/Original.jpg')
As can be seen, at the second parameter, the name contains characters like ' and arises an error.
How can I avoid this?
Never output text from PHP directly into a Javascript context. As you're finding out, it's VERY easy to generate JS syntax errors.
Always use json_encode: e.g. given this
<?php $foo = 'bar'; ?>
<script>
var badly_broken = <?php echo $foo ?>;
var working_fine = <?php echo json_encode($foo); ?>;
</script>
You'll end up with
<script>
var badly_broken = bar; // oops - undefined variable "bar"
var working_fine = "bar";
</script>
And note that if you're outputting JS into an HTML attribute, you not only have to generate valid Javascript, you have to output valid HTML AS WELL:
<?php $foo = array('bar' => 'baz'); ?>
<a onclick="brokenCall(<?echo json_encode($foo) ?>)">
<a onclick="workinCall(<? echo htmlspecialchars(json_encode($foo)) ?>)">
produces:
<a onclick="brokenCall({"bar":"baz"})">
^--start attribute
^--end attribute - ruhroh
<a onclick="workingCall({"bar":"baz"}")>

document.title funcion can't set it work with php

I have this line of code
<script type="text/javascript">
document.title = "<?php include('pot1.php');?>"
</script>
My pot1.php gives me an echo with one random number, but that code does not seem to work and I don't know why the echo doesn't appear in my title. Is anything wrong with the code?
pot1.php code:
<?php
#include_once('link1.php');
$cg = fetchinfo("value","info","name","current_game");
$cb = fetchinfo("cost","games","id",$cg);
$cb=round($cb,2);
echo '$'.$cb;
?>
This echo is working and is giving me values because I can see them in one div.
You should just do it like this:
<?php include('pot1.php');?>
Now we know that there is a variable set. Echo that into the title tag.
<script type="text/javascript">
document.title = "<? echo $cb ?>"
</script>
The way you're doing it now... I don't think you can include an entire PHP page and expect to behave like a simple string, even if that's all it returns.
Put the value you want inside a variable in the pot1.php file and use it on the title
<?php include('pot1.php');?>
<script type="text/javascript">
document.title = "<?php echo $yourVar;?>"
</script>

using php _GET within javascript function

I have some javascript embedded into an html file that I am running in a browswer.
document.getElementById('home-search-text-inp').value = <?php echo htmlspecialchars($_GET['search_for']); ?>;
Why does this not fill the textbox?
Note that:
document.getElementById('home-search-text-inp').value = "hi";
puts "hi" into the textbox and:
<?php echo htmlspecialchars($_GET['search_for']); ?>
writes text just fine.
Thanks in advance
You're missing quotes around your string value:
document.getElementById('home-search-text-inp').value = <?php echo htmlspecialchars($_GET['search_for']); ?>;
^^^^ ^^^^
HERE HERE
should be:
document.getElementById('home-search-text-inp').value = "<?php echo htmlspecialchars($_GET['search_for']); ?>";

Create a JS Pop-Up to a link with a Data from Database

I have here my href link being echoed:
echo "<td><a href='../php/borrowersname.php?acc_number=".$row['acc_number']."'>".$row['title']."</a></td>";
And I have here a sample on how to create a Pop-Up:
Test
How to combine this to my href link above being echoed with the "javascript ... ..." enclosed by ("" && '')?
Thanks for another new learning.
You can escape the ""
<?php
echo "Test";
?>
In the 'borrowersname.php' page, you can get the value of 'acc_number' like this:
<?php
$value = $_GET['acc_number'];
//do something with $value;
?>

Categories