I have one if-else and I want to call function which contain jquery script. but its not working
Here is my codeignitor view code
Code
if($osoption == "windows")
{
?>
<script>
windows();
</script>
<?
$mysqldatabasehidden="N/A";
if ($_POST['mssqldatabasehidden']=="")
{
$mssqldatabasehidden = "1 No";
}
else
{
$mssqldatabasehidden = $_POST['mssqldatabasehidden']." No";
}
if($_POST['mssqlstoragehidden']=="")
{
$mssqlstoragehidden = "100";
}
else
{
$mssqlstoragehidden = $_POST['mssqlstoragehidden']." MB";
}
if($_POST['mssqltotalcosthidden'] =="")
{
$mssqltotalcosthidden = "3000 INR";
}
else
{
$mssqltotalcosthidden = $_POST['mssqltotalcosthidden'].".00 INR";
}
//echo ("mssql database =".$mssqldatabasehidden." <br>");
//echo ("mssql storage =".$mssqlstoragehidden." <br>");
//echo ("mssqltotalcost =".$mssqltotalcosthidden." <br>");
}
else
{
?>
<script>
linux();
</script>
<?
if($_POST['mysqldatabasehidden'] =="")
{ $mysqldatabasehidden = "0 No.";
$mysqldatabasecost="0.00 INR";
}
else
{
$mysqldatabasehidden = $_POST['mysqldatabasehidden']." No";
$mysqldatabasecost="0.00 INR";
//echo ("mysqldatabse =".$mysqldatabasehidden." <br>");
}
}
If windows selected than its call function name "windows" and same for "linux" when its in Else condition.
Here is my two functions
JQ Functions
<script>
function windows()
{
alert();
$("#divmssqldb").removeClass("divhide").addClass("divshow");
$("#divmssqlstor").removeClass("divhide").addClass("divshow");
$("#divmssqlprice").removeClass("divhide").addClass("divshow");
$("#divmysql").addClass("divhide");
}
function linux()
{
alert();
$("#divmssqldb").removeClass("divshow").addClass("divhide");
$("#divmssqlstor").removeClass("divshow").addClass("divhide");
$("#divmssqlprice").removeClass("divshow").addClass("divhide");
$("#divmysql").removeClass("divhide").addClass("divshow");
}
</script>
Remove the script which is written in your PHP code and call your functions after there definitions and declarations like,
<script>
function windows(){
alert('windows');
....
}
function linux(){
alert('linux');
....
}
$(function(){// add document ready function
<?php if($osoption == "windows") { ?> windows(); <?php } ?>
<?php if($osoption == "linux") { ?> linux(); <?php } ?>
});
</script>
Related
I want to return data only from the php function not whole page data.
Here is my demo.php file -
<?
echo "Whole Page data";
$name = $_POST['name'];
if ($name == "demo"){
demo();
}
else{
echo "No";
}
function demo(){
echo "demo data";
}
?>
And my ajax funtion is this -
function myAjax () {
$.ajax( { type : 'POST',
url : 'demo.php',
data : "name=demo",
success:function(data){
alert (data);
}
});
}
But it return whole page data with function data but I want only function data.
Check like,
if (!empty($_POST)) {
$name = $_POST['name'];
if ($name == "demo") {
demo();
} else {
echo "No";
}
die;
}
function demo()
{
echo "demo data";
}
This die; is important to halt execution process.
Give it a try, this should work.
I wan to call a PHP function in JavaScript code that echoes out a value:
<?php
function ex()
{
$num=10;
echo($num);
}
echo '<script type="text/javascript">
function sam_click(clicked)
{
var x="<?php ex(); ?>";
alert(x);
return false;
}
</script>'?>
However, it doesn't work. Can you help?
Try this:
<?php
function ex()
{
$num=10;
echo($num);
}
echo '<script type="text/javascript">
function sam_click(clicked)
{
var x="' . ex() . '>";
alert(x);
return false;
}
</script>'?>
Just try with:
...
var x="' . ex() . '";
...
Try echoing only what you need
<?php
function ex()
{
$num=10;
echo($num);
}
?>
<script type="text/javascript">
function sam_click(clicked)
{
var x="<?php ex(); ?>";
alert(x);
return false;
}
</script>
function ex()
{
$num=10;
echo $num;
return $num; // if you want to get JS alert
}
echo '<script type="text/javascript">
function sam_click(clicked)
{
var x='.ex().';
alert(x);
return false;
}
sam_click(); // if you want to get JS alert
</script>';
instead of echoing the $num, return it as a result and then append it in the script part.
<?php
function ex()
{
$num=10;
return ($num);
}
echo '<script type="text/javascript">
function sam_click(clicked)
{
var x="' . ex() . '";
alert(x);
return false;
}
</script>'?>
another solution with heredoc and nowdoc:
<?php
// helper to call functions as nowdoc
// example: {$fn( function() )}
function fn($functionCall)
{
return $functionCall;
}
$fn = 'fn';
function ex()
{
return 20;
}
$js = <<<EOT
<script type="text/javascript">
function sam_click(clicked)
{
var x="{$fn(ex())}";
alert(x);
return false;
}
</script>
EOT;
echo $js;
Find more detail on PHPDoc:
Nowdoc (PHP5.3+)
Heredoc
Anonymous Functions (PHP5.3+)
i have a link with the value of Active and Offline..
while($s = mysql_fetch_array($strength))
{
?>
<a href="updateperiode.php?id=<?php echo $s['periode_id']; ?>" onclick="return confirmReset()">
<?php if($s['status'] == 'Active')
{
?>
<span class="badge_style b_online">Active</span>
<?php
}
else
{
?>
<span class="badge_style b_offline">Offline</span>
<?php
}
?>
</a>
<?php } ?>
what i want to do is,, if the value is Active, and the user is click it, the alert with pop out.. and says do you want to deactive, and if the value is offline, when the user click, the alert will says do you want to activate ..
and my javascript is like this..
<script>
function confirmReset() {
var div = document.getElementById("div");
var span = div.getElementsByTagName("span");
if(span.innerHTML == 'Active')
{
var question = confirm("Are you sure want to deactive?");
}
else
{
var question = confirm("Are you sure want to activate?");
}
if(question){
return true;
}else{
return false;
}
}
</script>
the output is always Are you sure want to activate? even though the value is active..
This is my full code..
http://pastebin.com/FteRz6VN
this is the screen..
http://puu.sh/8CyBy.jpg
Please try this
<?php
$i = 0;
while($s = mysql_fetch_array($strength))
{
?>
<a href="updateperiode.php?id=<?php echo $s['periode_id']; ?>" onclick="return confirmReset('<?php echo 'status'.$i ?>')">
<?php if($s['status'] == 'Active')
{
?>
<span class="badge_style b_online" id="status<?php echo $i ?>">Active</span>
<?php
}
else
{
?>
<span class="badge_style b_offline" id="status<?php echo $i ?>">Offline</span>
}
?>
</a>
<?php $i++; } ?>
<script>
function confirmReset(id) {
var statusDiv = document.getElementById(id).innerHTML;
if(statusDiv == "Active"){
var question = confirm("Are you sure want to deactive?");
} else {
var question = confirm("Are you sure want to activate?");
}
if(question){
return true;
} else {
return false;
}
}
</script>
Change the span variable:
var span = div.getElementsByTagName("span")[0];
You can use jquery to solve this issue very easily following is the code:
$(document).ready(function() {
$(".badge_style").click(function(){
var isActive = $(this).html();
if(isActive == "Active"){
...Do what you want to do if active
alert("do you want to activate!");
}else{
... else part
}
});
});
no need to call confirmReset function
Basically I want a checkbox to onClick change whether or not to show explicit content. I'll get right to the code HTML (and PHP):
</form>
<?php if (login_check($mysqli) == true) {
$username = $_SESSION['username'];
echo '
<form name="explicit">
<input type="checkbox" onClick="changeexplicit();" value="Filter Explicit Content" id="explicitcheckbox" ' ;
if (checkadult($mysqli,$username)!=1){echo "checked";}
echo ' /> <label for="explicitcheckbox" class="explicit">Filter Explicit Content</label>
</form>';}?>
Jquery: (i left the error handling blank because I didn't realize JQuery doesn't have a built in popup alert)
<script>
changeexplicit()
{
!(function($){
$(function() {
$.post('includes/changeadult.php', $('form[name="explicit"]').serialize(), function(data){
data = $.parseJSON(data);
if(data.success){
window.location.reload(true);
}
else if(data.error){
}
else{
}
});
});
})(window.jQuery);
}
</script>
PHP:
<?php
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start(); // Our custom secure way of starting a PHP session.
$response = array();
if (login_check($mysqli) == true) {
$username=$_SESSION['username'];
$result=mysqli_query($mysqli,'SELECT isAdult FROM members WHERE username = "'.$username.'"');
$userinfo = mysqli_fetch_array($result);
$isAdult=$userinfo['isAdult'];
if ($isAdult==1)
{mysqli_query($mysqli,'UPDATE members SET isAdult=0 WHERE username="'.$username.'"');}
else{
{mysqli_query($mysqli,'UPDATE members SET isAdult=1 WHERE username="'.$username.'"');}}
$response['success'] = true;
}
else if(login_check($mysqli) == false) {
$response['error'] = true;
}
echo json_encode($response);
<script>
function changeexplicit(){
$.post('includes/changeadult.php', $('form[name="explicit"]').serialize(), function(data){
data = $.parseJSON(data);
if(data.success){
window.location.reload(true);
}else if(data.error){
}else{}
});
}
</script>
Try change onClick toonchange?
I have written a php file and jquery to retrieve data from a database and validate on a textfield blur event to check the typed value is whether available or not. Below are my code:
On form php:
<script>
$("#catname").blur(function() {
$.post("./scripts/checkavailability.php", {
nameava: $("#catname").val(),
}, function(data) {
alert(data);
});
var setr = "<?php
include './scripts/checkavailability.php';
$dbava = getfromdb("name", "tbl_category");
$avams = check($txtval, $dbava, "$name");
echo $avams;
?>";
$("#jinx").html(setr);
});
</script>
checkavalilability.php :
<?php
if (isset($_POST['nameava'])) {
$txtval = mysql_real_escape_string($_POST['nameava']);
}
function getfromdb($field, $table) {
$avres = mysql_query("SELECT `" . $field . "` FROM `" . $table . "`");
return $avres;
}
function check($curval, $qres, $s_field) {
while ($a_row = mysql_fetch_array($qres)) {
$dbval = $a_row[$s_field];
if ($curval == $dbval) {
return "This value is taken";
break;
} else {
return "This value is available";
}
}
}
?>
Note: catname is the textfield id and jinx is the div id.
I think you are trying something like this:
jQuery:
<script>
$("#catname").blur(function() {
$.post("./scripts/checkavailability.php", {
nameava: $("#catname").val(),
}, function(data) {
alert(data);
$("#jinx").html(data);
});
});
</script>
PHP:
<?php
function getfromdb($field, $table) {
$avres = mysql_query("SELECT `" . $field . "` FROM `" . $table . "`");
return $avres;
}
function check($curval, $qres, $s_field) {
while ($a_row = mysql_fetch_array($qres)) {
$dbval = $a_row[$s_field];
if ($curval == $dbval) {
return "This value is taken";
//break;
} else {
return "This value is available";
}
}
}
if (isset($_POST['nameava'])) {
$txtval = mysql_real_escape_string($_POST['nameava']);
$dbava = getfromdb("name", "tbl_category");
$avams = check($txtval, $dbava, "name");
echo $avams;
}
exit();
?>