How to run php code in javascript when callback is called? - javascript

I'm using easyModal to create modal windows in my web. Saw that they have callback onClose and I want use php code on close is it possible, if yes How to do that? I want to unset some values from sessions (don't have much experience in javascript, jquery or ajax)
This is my code:
<div id="ebox">
<?php
echo $_SESSION['unameE'];
echo $_SESSION['pwordE'];
echo $_SESSION['emailE'];
?>
</div>
$(function() {
$('#ebox').easyModal( {
autoOpen:<?php echo $error; ?>
onClose:???
});
});
Or maybe there is another solution that unsets values from session after displaying it once?

on your script you should have :
onClose: function(){
$("#ebox").html(' ') ;
$.post("remove-session.php");
}
and then on the page "remove-session.php" you can do all sort of stuff with your session:
<?php
unset($_SESSION['unameE']);
//...
//or even
session_destroy();
?>

Related

how to dynamically change bootstrap alert test using php?

HTML:
<div id="signUpError"></div>
JS:
<script type="text/javascript">
function showSignUpError(message) {
document.getElementById('signUpError').innerHTML="<div class='alert alert-danger'>"+message+"</div>";
}
</script>
PHP:
$error="this is error"
How to call showSignUpError($error)
Try this code:
showSignUpError(<?php echo $error; ?>);
Try that first, but it might be necessary to quote the text:
showSignUpError("<?php echo $error; ?>");
Note that this will only run once, on page load.
If you wish to call-out to the PHP code while interacting with user, then you should check out AJAX:
AJAX request callback using jQuery

wordpress ajax call single post

I tried to do a ajax call in my wordpress loop, basically i would like to load the content of the post in the home page in a div, after the click on the title.
the call ajax seems work, but i have the problem that load whole the page, and i would like to load only the content of a div with specific id.
this is my code: index.php
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; endif; ?>
<div id="single-post-container"></div>
this is the single.php
<?php $post = get_post($_POST['id']); ?>
<div id="single-post post-<?php the_ID(); ?>">
<?php while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_content();?>
<?php endwhile;?>
</div>
and this is the js
jQuery(document).ready(function(){
jQuery.ajaxSetup({cache:false});
jQuery(".post-link").click(function(){
var post_link = jQuery(this).attr("href");
jQuery("#single-post-container").html("content loading");
jQuery("#single-post-container").load(post_link);
return false;
});
});
i tried to add the id #single-post after post_link like this:
jQuery("#single-post-container").load(post_link #single-post);
but when i run gulp i have an error that block me.
how can i achieve this scope?
someone could give me an help or suggestion?
I think this may be the answer but only if I have correctly understood the problem:
What is post_link? What actual url is your AJAX call calling? If you are just calling single.php the first thing that does is call header() - hence you might get the whole page. Looking at your index.php i think this may be what is happening.
If so. You need to look up how to do AJAX calls in WordPress. You have to add an AJAX callback with something like add_action('wp_ajax_my_action', mycallback). Then your callback can just get the single post with get_post(). The actual url your AJAX call should be calling is something like:
yourdomain/wp-admin/admin-ajax.php?action=my_action&post_id=THEPOSTID. You can use admin_url() to help you build the correct url.
You may need to inject THEPOSTID into your JavaScript. Using wp_localize_script. Or you could add it as an attribute in index.php and get it in your JavaScript.
Then your callback - mycallback() - can use get_post() to get just the post and return it without all the page fluff. E.g.:
function mycallback()
{
$postId = $_GET['post_id'];
$postRow = get_post($postId);
echo $postRow->post_content;
wp_die();
}
Maybe look for a few examples on how to do AJAX with WordPress? e.g. http://wptheming.com/2013/07/simple-ajax-example/
UPDATE:
added wp_die(); at the end of the AJAX call-back. You need to do this to stop WordPress spitting out a lot of page bumf as well. In fact this may be the part that is missing from your single.php. Stil, probably better to do to it using admin-ajax.php anyway perhaps.
if this can help someone, i solved with this code
//take the permalink of the single post
var post_permalink = locations[i].post_url + " #single-post";
// load post in the div
jQuery("#single-post-container").html("content loading");
jQuery('#single-post-container').load(post_permalink);
thanks you all anyway

Why yii captcha always shows a fixed picture?

Captcha works with no problem, but I have no idea why it's not generating a new code to dislay? I've looked into the documents but could find something that could solve my problem.
Is there something here that I'm missing. thanks.
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
);
}
my view file:
<?php echo $form->labelEx($model,'verifyCode'); ?>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
<?php echo $form->error($model,'verifyCode'); ?>
This is a known bug, that would most likely be fixed in Yii2.
On the Yii forums, user Black suggests:
My solution was to remove the session key on my controller action on get. Be careful not to remove it in any other place because it will probably fail on server validation.
$session = Yii::app()->session;
$prefixLen = strlen(CCaptchaAction::SESSION_VAR_PREFIX);
foreach($session->keys as $key)
{
if(strncmp(CCaptchaAction::SESSION_VAR_PREFIX, $key, $prefixLen) == 0)
$session->remove($key);
}
Another way to workaround would be to use JavaScript to click on the refresh link on every page load as mentioned by Soph:
$(function() {
$('#yw0_button').click();
});

Clicking on a div and saving its value to use with PHP

I have been having some problems with some code.
So I'm developing a website that accesses a mysql database and creates a number of divs accordingly to the number of specialties that are on the database.
<div id="services_panel">
<?php foreach ($specialties as $especialidade_id => $especialidade) { ?>
<div class="service_div" onclick ="highlightLink(this); $('#content_div').show();" value="<?php echo $especialidade_id; ?>"><?php echo $especialidade; ?></div>
<?php } ?>
</div>
<div id="content_div""/>
Now what I wanted to do was set the value of the content div to the value of $especialidade of the div that was clicked, but since javascript runs clientside and php runs server side, I know that I can't do it on onclick()
I'd really hope you guys could help me with this issue
Thanks in advance
Using onclick() inline you could do
onclick ="highlightLink(this); $('#content_div').show().html(this.innerHTML);"
or if you bind it separately using .on() and 'click'
$(function(){
$('.service_div').on('click',function(){
$('#content_div').show().html($(this).html());
});
});

How would I call a java script function using php if statement with $_SESSION

Hi I am creating a website with a login section this is working I am using HTML and PHP. What I am trying to do is one of my pages has a html button I want this to be disabled for certain users. at the moment this is what I have got.
this is the part that I use for the login details.
<?php
session_start();
$_SESSION["username"];
$_SESSION["password"];
$_SESSION["access"];
?>
I have got if statments that I am currently using which are
if($_SESSION["access"] == "Administrator"){
echo $Admin;
}
what I am trying to do is call a javascript function within a PHP if statement what i have got so far is
<?php
if($_SESSION["access"] == "Consumer")
{
echo '<script type="text/javascript">
Disable();
</script>';
}
if($_SESSION["access"] == "Administrator")
{
echo '<script type="text/javascript">
Enable();
</script>';
}
?>
the javascript functions that i am trying to call are
<script type="text/javascript">
function Enable() {
SubmitButton.disabled = false;
}
function Disable() {
SubmitButton.disabled = true;
}
</script>
I have also tryed
if($_SESSION["access"] == "Consumer")
{
echo "<script> Disable(); </script>";
}
Im just wondering if I have typed something in wrong or if I have forgotten to put something in.
any help would be much appreciated.
Looking at your code you have couple of issues:
Mixing your PHP logic and pure HTML is (usually) not a good idea.
Instead I would suggest you move your access checking logic fully on the server side and display the button accordingly (disabled or enabled) based on the user's access.
Example:
<?php if($_SESSION['access']): // Only show the button for users with access ?>
<button type="submit" value="Submit" <?php echo ($_SESSION['access'] != 'Administrator' ? 'disabled' : ''); // Button disabled for everyone but administrators ?> />
<?php endif; ?>
And let me point out the obvious (as mentioned by the other answers), that's not 100% bulletproof. The user can still manually submit the button even if he is not an administrator by editing the page's HTML on the fly. That's just a UI fix. The real check should be done on the server side once the button is submitted (e.g. is the user logged in, does he have a cookie on his computer that identifies him as an administrator, does he have a session cookie set, etc).
Calling JS in random places, e.g. in the header can have unexpected consequences.
You better wait for the page to be loaded fully before calling any JS functions. You can do that via jQuery easily, but make sure you include the jQuery library before that in your header like so.
Afterwards you can call any JS after the page is loaded by placing them within the following block:
$(function(){
// Place your JS calls here, e.g. call to Enable()
});
String concatenation in PHP is done with a dot . and strings can be multiline
This code which you used is just plain wrong.
echo '<script type="text/javascript">'
, 'Enable();'
, '</script>';
You should use something like:
echo '<script type="text/javascript">'
.'Enable();'
. '</script>';
or better:
echo '<script type="text/javascript">
Enable();
</script>';
PHP doesn't use , sign for joining. Use ..
But otherwise it should work, except that you should define SubmitButton in advance of using it.
<?php
echo "<script type='text/javascript'>";
// if the id of your element is "submitButton"
echo "var submitButton = document.getElementById('submitButton');";
echo " function disable(){ submitButton.disabled=true; }";
echo "</script>";
?>
After that you can use it as you did..
<script type='text/javascript'>
disable();
</script>
Just be advised that denying access to some elements/functionality on your webpage with JavaScript alone is not a good practice - JavaScript is executed locally on the user's computer and therefore the user can modify it to gain an advantage.
Well, the problem may be that you're trying to call the javascript function before the HTML is ready (or finally rendered), so the browser, when executes the function doesn't find the button.
You could solve this placing your javascript code at the end of your page, or using jQuery and doing:
$(document).ready(function() {
<%php if ($_SESSION['access'] == 'xxxxx') {%>
Enable();
<%php } else { %>
Disable();
<%php } %>
});
Anyway, ALWAYS check user permissions on the server side, because someone could enable the button using Firebug or something else...

Categories

Resources