I'm having some trouble with selecting the radio option affiliated to the query string. I have the following code in place...
<?php if ( $_GET['fp'] == 'floorplanfive' ) { ?>
<script type="text/javascript">
jQuery('#choice_2_4').attr('checked', 'checked');
console.log("test");
</script>
<?php } ?>
The console is showing the message 'test' but the radio option is not being selected.
The page can be found here.
Please help out. :-)
You need to put your code inside of the $(document).ready() function:
jQuery(document).ready(function() {
// your code
});
Use this:
jQuery('#choice_2_4').prop('checked', true);
Edit: if your radio button is loaded after this code, use it:
jQuery(document).ready(function () {
jQuery('#choice_2_4').prop('checked', true);
}
Use == comparison operator instead of = assignment.
Assignment in if condition will always return true.
Change your code to:
<?php if ( $_GET['fp'] == 'floorplanfive' ) { ?>
<script type="text/javascript">
jQuery('#choice_2_4').attr('checked', 'checked');
console.log("test");
</script>
<?php } ?>
Try Following:
jQuery('#choice_2_4').attr('checked', true).checkboxradio("refresh");
Related
How do I load a script if a PHP condition is true?
For example, I got an external script called script.js and I wanna load this if it's on the product page of WooCommerce.
I've tried the following but it doesn't work because it prints the code on the page:
<?php
add_action( 'template_redirect', 'check_product_page' );
function check_product_page() {
if(is_product()) {
include ('script.js');
}
}
?>
If I write the script inside a PHP like the below, it causes a fatal error:
<?php
echo
'<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>';
?>
Try this:
<?php
if($condition == true){
echo '<script type="text/javascript" src="script.js"></script>';
}
?>
or:
<?php if($condition == true): ?>
<script type="text/javascript" src="script.js"></script>
<?php endif; ?>
Actually this works on my side, checkout maybe you have another issue:
echo '<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>';
It's because the single quotes are inside your string. You should actually do it the way which other people have shown, but if you need it to be an inline script you can do it like this:
<?php
//some php can go here..
?>
<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>
<?php //more php can go here... ?>
I have a page that receives incoming values from $_POST. One such value is from a drop down selector that allowed the user to select values 0 -10. I want to trigger a JQuery function if a value greater than 0 was selected.
So, if $_POST['DropDownSelection'] > 0, then my JQuery function should run.
How do I trigger the function?
If the function needs to be called in the original page then you can do this -
$('select[name="DropDownSelection"]').change(function() {
var newValue = $(this).val();
if(newValue > 0) {
// your function here
}
});
You don't need PHP, you just need to see if the value changed and then if the value is greater than 0.
If the function is in the page that gets posted to then you could do this -
<script>
var DropDownSelection = <?php echo $_POST['DropDownSelection']; ?>;
if(DropDownSelection > 0) {
// call your function here
}
</script>
Something i like to do for passing a PHP var to Javascript is to put in in an hidden input like that :
<input id="myValue" type="hidden" value="<?= $_POST['DropDownSelection']; ?>" />
The in your javascript :
if(document.getElementById('myValue').value > 0) //Do something
Depends on how your PHP code is connected to the HTML output. In the simplest case where PHP and HTML are in the same file, you could do something like
<? if ($_POST['DropDownSelection'] > 0) { ?>
<script>$.myFunction(...);</script>
<? } ?>
You can do like that.
var x = <?php echo $_POST['DropDownSelection'] ?>;
if(x>0){
jqueryFunction(); // your function call.
}else{
// whatever else you want.
}
Maybe this is oversimplified and a little hacky, but I don't see why this wouldn't work...
<script type="text/javascript">
function overZero() {
// do stuff...
}
<?php
if ($_POST['DropDownSelection']>0) echo('overZero();');
?>
</script>
$(document).ready(function(){
$("#dropdown-id").change(function(){
//check if the value is > 0 and then
// trigger your jquery function()
})
})
However, I want to also call that function when the user lands on the page with
a particular $_POST value for a field
There are 2 easy ways of doing this:
Make global funciton:
ex:
function print(){
alert('hello')
}
<?php
if($_POST['DropDownSelection'] != 0)
{
echo "<script>print()</script>";
}
?>
Or use triger function from jquery:
<?php
if($_POST['DropDownSelection'] != 0)
{
echo "<script>$('#dropdown').trigger('change');</script>";// execute the onchange event(function) attached to the dropdown
}
?>
I would like to submit a <form> automatically if:
if(isset($_POST['action']) && ($_POST['action'] =='confirmado')){
submit form.
I don´t know if I should use a javascript script.
I try doing this but it is not working.
<script>
if(isset($_POST['action']) && ($_POST['action'] =='confirmado')){
$("#form").submit(function(){
document.form.submit();
return false;
}
</script>
You are trying to mix up PHP, which is a server-side language, with Javascript, which is a client-side language. So, that won't work.
Moreover, the submission of a <form> is NOT captured in its $_POST array as $POST['action']. The method of submission is POST and the $_POST array contains data submitted via the form's html elements.
Try this:
$( '#form' ).submit(function () {
if ( this.action !== 'confirmado' ) return false;
});
First of all, I agree with coder1984: you are mixing PHP with Javascript, and you are trying to access POST data in the client side, which is not possible. If I can guess what you are trying to do, maybe this code will help you:
<script type="text/javascript">
if (<?php echo (isset($_POST['action']) && ($_POST['action'] =='confirmado')) ? '1' : '0'; ?>) {
document.form.submit();
}
</script>
If i understand your code right, you would like to only submit the form if the field "action" equals "confirmando". Since you are allready using jQuery let's keep at it.
JavaScript:
<script>
$('document').ready(function() {
$('input[name=action]').change(function() {
if($(this).val() == 'confirmando') {
$('form').submit(); // If you would like to do a normal sumbit
$.post('my_php_file.php', $('form').serialize(), function(data)) { // or use AJAX
/* var data now contains the output of the PHP file */
}
}
});
});
</script>
It still makes sense to check the submited form in PHP when you receive the data from the form.
learning Jquery and integrating with PHP - getting there, but have one last challenge in some code I'm working on.
I have HTML in a string, trying to pull html in tags, might be multiple elements in the HTML string, so trying to use each. My function worked fine without each, below is my each integration (returns nothing currently):
<?php
$info = '<li><strong>I want this text</strong></li><li><strong>I want this text too</strong></li>';
$info = json_encode($info);
?>
<script type="text/javascript">
$(document).ready(function () {
$("a", $( < ? php echo $info; ? > )).each(
function () {
alert($(this).html());
});
};
This code below does work, but only returns the first element in the HTML:
<?php
$info = '<li><strong>I want this text</strong></li>';
$info = json_encode($info);
?>
<script type="text/javascript">
$(document).ready(function () {
var output = $("a", $( < ? php echo $info; ? > )).html();
var link = $("a", $( < ? php echo $info; ? > )).attr("href");
alert(output);
alert(link);
});
</script>
This is a description and a working example of How to use .each() LINK
You can try this one as a example
$("a").each(function(index){alert($(this).html()});
Your code is not working because there are a few syntax issues.
First, change
< ? php echo $info; ? >
to
<?php echo $info; ?>
PHP doesn't like spaces and the opening and closing tags must appear without spaces.
Second, close the ready function and the script tag properly. Instead of
};
use,
});
</script>
Why are you encoding a piece of XML with JSON? That makes like no sense at all. Both are ways to encode data. HTML is XML too, btw. You can directly reference the $info variable since PHP will process everything first on the server.
<?php
$info = '<li><strong>I want this text</strong></li>';
?>
<script type="text/javascript">
$(document).ready(function() {
$("a", $("<?php echo $info; ?>")).each(
function () {
alert($(this).html());
}
);
});
Or just remove the temporary variable altogether. Makes it combersome to read, but that's essentially what PHP is doing.
$("a", $("<?php echo '<li><strong>I want this text</strong></li>'; ?>")).each(
Or to make it even simpler, since you already have the HTML, simply include it as part of the page and maybe give it an ID to make referencing it easier with jQuery.
<li id="myList">
<strong>I want this text</strong>
</li>
<script type="text/javascript">
$(document).ready(function() {
$("a", "#myList").each(function() {
alert($(this).html());
});
});
</script>
This last example gets rid of PHP completely, but you weren't really using it anyways.
How do i do in jquery, to check if there's anything in isset SESSION user_message, each 1 seconds, and if there is then display #box else dont..
thanks
PHP file (session.php):
<?php if( isset($_SESSION) ){echo 1;}?>
JS file:
function checkSession(){
$.ajax({url: "session.php", success: function(data){
if( data == 1){
$('#box').show();
}else{
$('#box').hide();
}
}});
}
setInterval('checkSession()',1000);
This is untested, but it should do what you're looking for. Note that, typically, AJAX request expect XML or JSON, so you can reformat the session.php file the way you want.
Use PHP to echo the jQuery command:
if (isset($_SESSION['user_message'])) {
echo '<script> $("#box").… </script>';
}
This will only echo the following if $_SESSION['user_message'] is set:
<script> $("#box").… </script>
Simple as this:
<?php
if (isset($_SESSION['user_message']))
{
?>
<script>
$('#box').show(2000);
</script>
<?php
}
?>
Or you can use fadeIn or fadeOut with specified time.
<?php if (isset($_SESSION['user_message'])) { ?>
<script type="text/javasscript">
$("#box").show(1000);
</script>
<?php } ?>