I am trying to get into some PHP and I can't seem to figure out the following.
I can create a string by means of PHP with concatenation:
echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";
But what if you want to do this with a variable? (I am guessing WP's functions can be called variables.) I am quite lost here. The following does not work. Or at least, my text editor throws an unknown error.
<?php
if ( is_404() ) {
echo "<script src='" . get_stylesheet_directory_uri(); . "/sm/script/soundmanager2-nodebug-jsmin.js'></script>
<script>
soundManager.setup({
url: '" . get_stylesheet_directory_uri(); . "/sm/swf/',
onready: function() {
var mySound = soundManager.createSound({
id: 'aSound',
url: 'http://www.mysite.com/sound.mp3',
volume: '25'
});
mySound.play();
},
ontimeout: function() {
}
});
</script>"
}
?>
It goes wrong here:
rc='" . get_stylesheet_directory_uri(); . "/s
You have a ; in the middle of your sentence. Also at the end of your string you don't have a ;
Correct syntax
<?php
if ( is_404() ) {
echo "<script src='" . get_stylesheet_directory_uri() . "/sm/script/soundmanager2-nodebug-jsmin.js'></script>
<script>
soundManager.setup({
url: '" . get_stylesheet_directory_uri() . "/sm/swf/',
onready: function() {
var mySound = soundManager.createSound({
id: 'aSound',
url: 'http://www.mysite.com/sound.mp3',
volume: '25'
});
mySound.play();
},
ontimeout: function() {
}
});
</script>";
}
?>
You can also change your approach to avoid such problems. Instead of embeding HTML into PHP you can embed PHP into HTML closing and opening PHP tags:
<?php if ( is_404() ) { ?>
<script src="<?php echo get_stylesheet_directory_uri(); ?>/sm/script/soundmanager2-nodebug-jsmin.js"></script>
<script>
soundManager.setup({
url: "<?php echo get_stylesheet_directory_uri(); ?>/sm/swf/",
onready: function() {
var mySound = soundManager.createSound({
id: 'aSound',
url: 'http://www.mysite.com/sound.mp3',
volume: '25'
});
mySound.play();
},
ontimeout: function() {
}
});
</script>
<?php } ?>
Try this
if ( is_404() ) {
echo "<script src='" . get_stylesheet_directory_uri(). "/sm/script/soundmanager2-nodebug-jsmin.js'></script>
<script>
soundManager.setup({
url: '" . get_stylesheet_directory_uri(). "/sm/swf/',
onready: function() {
var mySound = soundManager.createSound({
id: 'aSound',
url: 'http://www.mysite.com/sound.mp3',
volume: '25'
});
mySound.play();
},
ontimeout: function() {
}
});
</script>";
}
you are adding ; in between the code which is incorrect.
Try this:
<?php
if ( is_404() ) {
echo "<script src='" . get_stylesheet_directory_uri() . "/sm/script/soundmanager2-nodebug-jsmin.js'></script>
<script>
soundManager.setup({
url: '" . get_stylesheet_directory_uri() . "/sm/swf/',
onready: function() {
var mySound = soundManager.createSound({
id: 'aSound',
url: 'http://www.mysite.com/sound.mp3',
volume: '25'
});
mySound.play();
},
ontimeout: function() {
}
});
</script>";
}
?>
You can save yourself a lot of headache if you simply save the return value of the function:
<?php
if ( is_404() ) {
$temp = get_stylesheet_directory_uri();
echo "<script src='$temp/sm/script/soundmanager2-nodebug-jsmin.js'></script>
<script>
soundManager.setup({
url: '$temp/sm/swf/',
onready: function() {
var mySound = soundManager.createSound({
id: 'aSound',
url: 'http://www.mysite.com/sound.mp3',
volume: '25'
});
mySound.play();
},
ontimeout: function() {
}
});
</script>"
}
?>
Related
I have a page with 12 check boxes one ever hr, the display OK, Trying to on change event to run on each
I can add 24 of them but looking for a better way this is what I have tried and does not work.
<script>
$(document).ready(function(e) {
var i = 0;
while ( i < 23)
{
$('#' + i).change(function(){
if( $('#'+ i).prop('checked') )
{checkboxstatus = "YES";}
else
{checkboxstatus = "NO";}
$.ajax({
type: "POST",
url: "timepost.php",
data: {checkbox: i ,user: <?php echo $iduser; ?>, checkboxstatus: checkboxstatus},
})
//.done(function(data, textStatus, jqXHR){alert(textStatus);})
//.fail(function(jqXHR, textStatus, errorThrown){alert(jqXHR+"--"+textStatus+"--"+errorThrown);});
});//end change
i++;
}
});//end ready
</script>
<?php include 'footer.php'; ?>
</body>
</html>
<?php
function tr($row,$hr)
{
if ($row)
{
$checked = "checked";
}
else
{
$checked = "";
}
print ("<td><input type=\"checkbox\" id=\"" . $hr . "\" name=\"" . $hr . "\" " . $checked . "></td>\r\n");
}
?>
There is no need to do a while loop here, jQuery can find all elements for you. Attached code should work
<script>
$(document).ready(function(e) {
// jQuery will find all elements with class checbox-class
$('.checkbox-class').on('change', function(){
if ($(this).prop('checked')) {
checkboxstatus = "YES";
} else {
checkboxstatus = "NO";
}
$.ajax({
type: "POST",
url: "timepost.php",
data: {checkbox: $(this).attr('id') ,user: <?php echo $iduser; ?>, checkboxstatus: checkboxstatus},
})
//.done(function(data, textStatus, jqXHR){alert(textStatus);})
//.fail(function(jqXHR, textStatus, errorThrown){alert(jqXHR+"--"+textStatus+"--"+errorThrown);});
});//end change
});//end ready
</script>
<?php include 'footer.php'; ?>
</body>
</html>
<?php
function tr($row,$hr)
{
if ($row)
{
$checked = "checked";
}
else
{
$checked = "";
}
print ("<td><input type=\"checkbox\" class=\"checkbox-class\" id=\"" . $hr . "\" name=\"" . $hr . "\" " . $checked . "></td>\r\n");
}
?>
I have some checkboxes, that when checked adds their respective classes to the <body> element via Ajax:
<input type="checkbox" name="<?php echo $opt;?>" class="onoffswitch-checkbox" id="myonoffswitch-<?php echo $n;?>" value="true"<?php echo (dh_switch_setting_get($opt, '0') == '0' ? "" : " checked='checked'");?> />
<label class="onoffswitch-label" for="myonoffswitch-<?php echo $n;?>" data-class="<?php echo $obj['class'];?>"></label>
(function($) {
$(function() {
$("LABEL[for^=myonoffswitch]")
.each(function() {
// ---Which label was clicked? ---
var which = $(this).attr("for");
var cls = $(this).data("class");
if ( $("INPUT[id=" + which + "]").is(":checked") ) {
$("BODY").addClass(cls);
} else {
$("BODY").removeClass(cls);
}
$(this).parents(".onoffswitch").on("click", function() {
if (!$("INPUT[id=" + which + "]").is(":checked")) {
$("BODY").addClass(cls);
} else {
$("BODY").removeClass(cls);
}
var val = $("INPUT[id=" + which + "]").is(":checked") ? "1" : "0";
console.info("Set " + which + " to " + val)
$.ajax({
url: './',
type: "POST",
data: $("INPUT[id=" + which + "]").attr("name") + "=" + val + "&<?php echo MD5("DH");?>=1",
success: function (response) {
},
error: function (jqXhr, e, responseText) {
console.log(arguments);
alert("Oops. Something went wrong: " + responseText);
}
});
});
});
});
})(jQuery);
I also have a dynamic PHP stylesheet style.php as below:
<?php
header("Content-type: text/css;charset: UTF-8");
require('styles1.php');
require('styles2.php');
require('styles3.php');
?>
I'm trying to get some IF statements to work where if the body has a class, then require() the specified styles.
I tried variations of this, but nothing is working:
<?php
header("Content-type: text/css;charset: UTF-8");
$classes = get_body_class();
if (in_array('class1',$classes)) {
require ( 'styles1.php' );
}
if (in_array('class2',$classes)) {
require ( 'styles2.php' );
}
if (in_array('class3',$classes)) {
require ( 'styles3.php' );
}
?>
Can anyone help me out here?
I am using the code bellow of foxycomplete to get some results from my db with images.
With local files works great but i cant do it with mysql.
Here is my code that i am using..
<form id="autocompleteForm" name="autocompleteForm" action="" method="post">
<input type="text" name="s" id="s" value="Search" style="border:none;" />
</form>
The Javascript Code:
(function($) {
$(document).ready(function() {
$( '#s' ).each( function(){
$(this).attr( 'title', $(this).val() )
.focus( function(){
if ( $(this).val() == $(this).attr('title') ) {
$(this).val( '' );
}
} ).blur( function(){
if ( $(this).val() == '' || $(this).val() == ' ' ) {
$(this).val( $(this).attr('title') );
}
} );
} );
$('input#s').result(function(event, data, formatted) {
$('#result').html( !data ? "No match!" : "Selected: " + formatted);
}).blur(function(){
});
$(function() {
function format(mail) {
return "<a href='"+mail.permalink+"'><img src='" + mail.image + "' /><span class='title'>" + mail.title +"</span></a>";
}
function link(mail) {
return mail.permalink
}
function title(mail) {
return mail.title
}
$("#s").autocomplete(completeResults, {
width: $("#s").outerWidth()-2,
max: 5,
scroll: false,
dataType: "json",
source: "video_search.php",
matchContains: "word",
parse: function(data) {
return $.map(data, function(row) {
return {
data: row,
value: row.title,
result: $("#s").val()
}
});
},
formatItem: function(item) {
return format(item);
}
}).result(function(e, item) {
$("#s").val(title(item));
//location.href = link(item);
});
});
});
})(jQuery);
And the php code to retrieve my data:
<?php
require "connectiondb.php";
if(isset($_REQUEST['s']))
{
$queryString = mysql_real_escape_string($_REQUEST['s']);
echo'var completeResults = [{';
$ss = mysql_query ("SELECT title, image FROM users WHERE title LIKE '$queryString%' LIMIT 7");
while($result = mysql_fetch_assoc($ss)) {
echo '
"permalink": "index.html",
"image": "'.$result['image'].'",
"title": "'.$result['title'].'"
},';
}
echo '}];';
}else{
}
?>
Thank you Guys!!!
Whats inside the $result['image']? If it contains the image file itself(binary data) and not just the path, then you need to create one more file (image.php?s=id) which would retreive just that binary data with adequate header.. ie
<?php
header('Content-Type: image/jpeg');
// sql stuff
echo $result['image'];
?>
In your main file you'll then call just
"image": "'.image.php?id='.$result['id'].'",
also, dont torture yourself and use json_encode() :)
I have a form that using ajax to post the comment to video_comment.php then show up the comment live on video page.
This is my jquery code:
$("input[id*='post_video_playallcomment_']").livequery('click', function(event) {
event.preventDefault();
var video_msg = $("#post_message");
var input_id = $(this).attr('id');
var id_split = input_id.split('_');
var video_id = id_split[3];
var comment = $("textarea[id='video_comment']").val();
var response_messages = '#video_response';
if ( comment == '' ) {
video_msg.fadeIn();
return false;
}
video_msg.hide();
user_posting(response_messages, '<?php echo $lang['600']; ?>', 1);
reset_chars_counter();
$.post(base_url + '/video_comment.php', { video_id: video_id, comment: comment },
function(response) {
if ( response.msg != '' ) {
$(response_messages).hide();
user_posting(response_messages, response.msg);
$("textarea[id='video_comment']").val('');
} else {
$(response_messages).hide();
user_response(response_messages, '<?php echo $lang['587']; ?>');
$(".no_comments").hide();
$("textarea[id='video_comment']").val('');
var bDIV = $("#comments_delimiter");
var cDIV = document.createElement("div");
$(cDIV).html(response.code);
$(bDIV).after(cDIV);
}
}, "json");
});
On video_comment.php, this is the response.code to show up the comment live on video page.
echo 'text...
echo '<div id="commentplayer"></div>';
echo '<script type="text/javascript">';
echo 'jwplayer("commentplayer").setup({';
echo 'file: "...url",';
echo 'width: "300",';
echo 'height: "225",';
echo 'provider: "http",';
echo 'startparam: "start",';
echo 'wmode: "transparent",';
echo 'controls: "true",';
echo 'stretching: "uniform",';
echo 'primary: "flash",';
echo 'autostart: "false",';
echo 'ga: {}';
echo '});';
echo '</script>';
echo 'text...
I have no problem to get 'text...', but i can't get '<script type="text/javascript">function...</script>' to show up live on video page.
Help please...
instead of placing javascript code in the php return, why dont you place it in the .post response?
$("input[id*='post_video_comment']").click(function() {
$.post('video_comment.php', function(response) {
$('#comment').html(response.code);
//place javascript code here
}, "json");
});
or placing a onload before your js code
$str = '<div id="commentplayer"></div>
<script type="text/javascript">
window.onload=(function() {
jwplayer("commentplayer").setup({
file: "...url",
width: "300",
height: "225",
provider: "http",
startparam: "start",
wmode: "transparent",
controls: "true",
stretching: "uniform",
primary: "flash",
autostart: "false",
ga: {}
});
});
</script>';
echo $str;
I am trying to append new comment to the old comment box .
Here is my code , Dont know why its not working
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$(".bsubmit").click(function () {
var id = $(this).parent().parent().attr("id");
var comm = document.getElementById(id).getElementsByClassName("commentadd")[0].value;
$.ajax({
type: "POST",
url: "comment.php",
data: {
id: id,
comm: comm
},
cache: false,
success: function () {
$('.addcomment').slideUp('slow', function () {
// POSTED IN DATABASE SUCCESS NOW APPEND The comment with other COMMENTS
document.getElementById(id).getElementsByClassName(".itemcomment").append('<div class="comm">
<a href="profile.php?id=<?php echo $_SESSION['
fbid ']; ?> class="comm_img"><img src=" <?php echo $_SESSION['
smallest ']; ?> width="50" height="50" /></a>
<p width="50"><strong>
</strong></p>
</div>');
});
$('#load').fadeOut();
}
});
return false;
});
});
});
</script>
I think the error is in the code inside the "slideUp" callback, so I think the correct way to do it is:
Using regular JavaScript:
$('.addcomment').slideUp('slow', function () {
var elem = getElementById(id).getElementsByClassName(".itemcomment")[0],
div = document.createElement("div"),
a = document.createElement("a"),
p = document.createElement("p"),
img = document.createElement("img"),
strong = document.createElement("strong");
div.className = "comm";
a.className = "comm_img";
a.href = "profile.php?id=<?php echo $_SESSION['fbid ']; ?>";
img.src = "<?php echo $_SESSION['smallest ']; ?>";
img.width = "50";
img.height = "50";
p.width = "50";
p.appendChild(strong);
a.appendChild(img);
div.appendChild(a);
div.appendChild(p);
elem.appendChild(div);
});
Using JQuery:
$('.addcomment').slideUp('slow', function () {
var html = '<div class=comm>';
html += '<a href="profile.php?id=' + "<?php echo $_SESSION['fbid'];?>" + 'class=comm_img>';
html += '<img src="' + "<?php echo $_SESSION['smallest']; ?>" + 'width="50" height="50" /></a>';
html += '<p width="50"><strong></strong></p></div>';
$(".itemcomment", "#"+id).append(html);
});
Hope it helps!