JQuery custom alert box dosnt run automatically - javascript

I'm using a jquery alert library named Apprise2, it works fine when im using it for alerts in my form validation function. all i want is to show alert on page load, i call it at the end of the page, but it dont work, this is what i use at the end of page :
<?php if ($_SESSION['lasterror'] == '1') {?>
<script>
Apprise('error-occured');
</script>
<?php } ?>
when im using "alert" instead of "apprise" it works and shows the alert, i linked to the library bellow and i hope you can help me with this problem. thanks and sorry for my english.

You can try to do this in doc ready handler:
<?php if ($_SESSION['lasterror'] == '1') {?>
<script>
$(function(){
Apprise('error-occured');
});
</script>
<?php } ?>
Point 2: this might be the issue:
You are testing a string which might be a number here:
<?php if ($_SESSION['lasterror'] == '1') {?>
//-----------------------------------^-------here

Related

Auto-fill form on WordPress

I am using the Scripts n Styles plugin with this code to auto-fill a logged in user's email into form on WordPress:
<script type="text/javascript">
window.onload = function () {
document.getElementsByName("Element-Name")[0].value = "<?php $current_user->user_email ?>";
}
</script>
However, when I refresh the page I get the following text (the quoted value in the code above) instead of the actual value.
<?php $current_user->user_email ?>
Any idea what I am missing here? Is this an issue with the plugin?
The first thing I see is that you don't output anything.
Try
<?php echo $current_user->user_email; ?>
But in your example it should have been just an empty value. There is something fishy with the PHP parser not detecting the PHP part.

Using Javascript function to hide button inside PHP

I am currently writing a website where I want to hide the the supervisor editing button if the supervisor field = N. I am able to get the query to display the correct output but I can't get the button to hide. Can anyone help me get it to hide whenever the page loads. I also under stand I should be using PDO's but we have not been taught that sadly.
HTML & PHP
<?php
$supervisorstatus = mysql_fetch_assoc($supervisorresult);
echo $supervisorstatus['supervisor'];
if($supervisorstatus !='Y') {
echo '<script type="text/javascript">',
'closesup();',
'</script>'
;
}
?>
JavaScript
function closesup(){
document.getElementById('supervisor').style.visibility="hidden";
}
Supervisor is the element id for the button.
This is how your code would be
<?php
$supervisorstatus = mysql_fetch_assoc($supervisorresult);
echo $supervisorstatus['supervisor'];
if($supervisorstatus['supervisor'] !='Y') {
echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#supervisor").hide();
});
</script>';
}
?>
I gave a example of implementation above but if you need to see it separate Here is the code to hide your supervisor id
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#supervisor").hide();
});
</script>
$supervisorstatus contains an array. You probably want: if($supervisorstatus['supervisor'] !='Y') {

Can't refresh one specific div after ajax load (MVC system)

I already checked all others answers from that questions, but no one seems to work.
He's my problem, I try to reload a div where I got a php variable.
I got MVC system, so i can't call other files when i want.
My controller doesn't receive arguments from ajax call ($_POST and $_FILES are not isset).
When I try to do other stuff after ajax call, it works (example: unlink a file)
<?php
$test = 'lksd';
...
if( some conditions )
{
$test= 'piof';
unlink(../images/myImage.jpg);
}
?>
...
<div id="test">
<?php
echo $test;
?>
</div>
And my ajax call is in a jquery plugin (http://www.dropzonejs.com/)
I found where i can do my stuff, and then i call this function:
$('#test').load(window.location + "#test");
My div doesn't refresh but my file is deleted, if you have any idea of what can i do to solve this problem...
Thanks in advance
Create the PHP detail you want to stand alone. Call it something like getDetail.php:
<?php
$test = 'lksd';
...
if( some conditions ) {
$test= 'piof';
}
echo $test;
?>
In your page, you can now load that content at any time:
<html>
<body>
<div id="test">
</div>
Reload
<script>
$(document).ready(function(){
$("#test").load("getDetal.php");
$("#reloadDetail").click(function(){
$("#test").load("getDetail.php");
});
});
</script>
</body>
</html>

How do I embed Javascript in PHP code?

I have a HTML form and PHP code. In my form, I have a textbox and a textarea. Within both, I have included the "disabled" option. I want both textboxes to remain disabled until the user decides to click the "Edit" button, in which case both textboxes should be enabled so changes can be made and the output once again saved. According to the research I have done, the only way to do this is to use javascript, so I have included the following code within my PHP;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "var oldnotes = document.getElementById('oldnotes');";
echo "oldnotes.disabled = false;";
echo "var record = document.getElementById('record');";
echo "record.disabled = false;";
echo "</script>";
}
I have also tried;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "$('#oldnotes').removeAttr('disabled')";
echo "$('#record').removeAttr('disabled')";
echo "</script>";
}
But no luck :(
I am not receiving any errors, the textboxes just remain disabled after I click the Edit button. Could anyone help me with this? Thanks in advance.
This is a better approach for these kind of problems :
if (isset($_POST['edit'])){
?>
<script type="text/javascript">
var oldnotes = document.getElementById('oldnotes');
oldnotes.disabled = '';
var record = document.getElementById('record');
record.disabled = '';
</script>
<?php
}
<?
<script language='javascript'>
(javascript code here)
</script>
?>
Try use onclick on your Button
<input type="button" name="edit" id="edit" onclick="document.getElementById('oldnotes').disabled=false; document.getElementById('record').disabled=false; return false;">
I hope it helps.
The second approach seems correct, but you're missing ; there. Also, you haven't put any newline characters.
Here's what I suggest:
<?php
if (isset($_POST['edit'])) {
?>
<script type="text/javascript">
alert("Check entry"); // Use this for checking if your script is reaching here or not
$('#oldnotes, #record').removeAttr('disabled');
</script>
<?php
}
?>
You don't need to echo the whole thing. You can simply put it out of the PHP and keep it inside a if block.
Also, I've kept the alert to check if your code structure is proper. If you're getting the alert on clicking the edit button, means you're on the right path. You just need to workout on the JS.
If not, something wrong with your edit button and the form POST
Use single quotes for script type
echo "<script type='text/javascript'>\n";
//javascript goes here
echo "</script>";
use this jquery code:
<script>
$('#oldnotes,#record').attr('disabled','')
</script>
If you want to use JS with PHP you can read here:
how to write javascript code inside php
However: it seems you want the JS to be called on it own accord upon evaluation in the php logic as per the implementation provided.
Technically speaking your JS simply states it should re-enable the controls. Rather add the JS functionality to the OnClick of the submitting control and it should work fine. This is a quicker way of testing the functionality as well.
<a href="javascript:void(0);" onclick="document.getElementById('oldnotes').disabled=false;" >
Once it is clicked it will hide the appropriate control instead of trying to write new JS on the fly that is not executed. This is tried and tested and should work fine for you.
Hope it helps!
you can always make a cookie
use document.cookie()
set the onclick=“” to changing the cookie
and make a script that runs and gets the cookie when you open the site (use window.onload = function(){/*code goes here*/})

Cakephp 2.x - hide/show a combobox

I try to hide/show a combobox, I have 2 combobox and I want the second to be hidden until the first change.
<?php echo $this->Form->input('combobox1'); ?>
<?php echo $this->Form->input('combobox2'); ?>
So I think I need to do something like :
$this->Js->get('#idcombobox2')->effect('hide'); to hide the combobox when the page just load.
And something like :
$this->Js->get('#idcombo1')->event('change', $this->Js->get('#idcombobox2')->effect('show')); cause if the first change I want to show the second.
but this doesn't work.
thanks for your help.
are you including jquery or other framework in the view?
echo $this->Html->script('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js');
and in case you're outputting it into the <head> with array('inline' => false), make sure to specify the script location in the layout:
echo $scripts_for_layout;
remember you also need to output the buffer in the view:
echo $this->Js->writeBuffer();
edit: the code that actually worked for me:
$event = $this->Js->get('#combo2')->effect('show');
$this->Js->get('#combo1')->event('change', $event);
edit 2: somehow i just got to hide the combo2 using a scriptBlock. so the whole code would go like this
$hide = $this->Js->get('#combo2')->effect('hide');
echo $this->Html->scriptBlock($this->Js->domReady($hide));
$show = $this->Js->get('#combo2')->effect('show');
$this->Js->get('#combo1')->event('change', $show);
echo $this->Js->writeBuffer(array('inline' => false));

Categories