I have some trouble to instert a condition inside the function(data). It works for the second condition (i.e. else) but not for the first one. In other words when data="success" in my php, nothing appears in my html file. Furthermore console.log(data) is "ReferenceError: data is not defined " in both cases even if it works for the second condition.
Also in my networks(console) in Firefox I can see the data send from php in the second condition but for the first one it says that the data was not saved.
$errors is an array in php.
My code in ajax:
$.post('register3.php', {
username : $("#username").val(),
password : $("#password").val(),
passwordconfirm : $("#passwordconfirm").val(),
email : $("#email").val()
},
function(data){
if(data=="success"){
$("#results").html(data);
}
else{
$("#errors1").html(data.username);
$("#errors2").html(data.password);
$("#errors3").html(data.email);
}
My code in php
<?php
if(!empty($errors)){
header('Content-Type: application/json; Charset=utf-8');
$data = json_encode($errors);
echo $data;
}
else {
$data='success';
echo $data;
}
?>
My code in HTML
<div class="flash2">
<ul>
<li><div id="errors1"> </div> </li>
<li><div id="errors2"> </div> </li>
<li> <div id="errors3"> </div> </li>
<li> <div id="results"> </div> </li>
<ul>
</div>
Related
How do I check if my database has more than 9 entries, and if it has, I can advance to the next page.
Currently I have this code, but I don't know how to call that in my view:
Here in the controller I have already called the checklogin and see if it has more than 9 entries.
Controller:
function quizzes(){
$this->load->model('Quiz_Model');
$data['categorys'] = $this->Quiz_Model->SearchAllCategorys();
$this->load->view('headerLogged');
$this->load->view('quizzes', $data);
$this->load->view('footer');
}
function proses_quiz($id_category){
$this->load->view('headerLogged');
$checkquiz=$this->Quiz_Model->checkquiz($id_category);
if($checkquiz > 9){
redirect('User_Ctr/quiz/').$id_category;
}else{
$data['error_quiz']="MEH";
$this->load->view('quizzes', $data);
}
$this->load->view('footer');
}
And here it's just the code to make the count:
Model:
public function checkquiz($id_category){
$this->db->select('id_question, COUNT(id_question) as total');
$this->db->from('questions');
$this->db->where('id_category', $id_category);
$result = $this->db->get();
return $result->result();
}
View:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="container">
<h1 class="my-4 text-center text-lg-left">Quizzes</h1>
<div class="row text-center text-lg-left">
<?php foreach($categorys as $cat): ?>
<div class="col-lg-3 col-md-4 col-xs-6" >
<a href="<?php echo 'quiz/'.$cat->id_category ?>" class="d-block mb-4 h-100" >
<img class="img-fluid img-thumbnail" src="<?php echo $cat->img_category ?>" alt="" style="height: 125px; width: 400px">
<?php
if(isset($error_quiz)){
echo $error_quiz;
}
?>
</a>
</div>
<?php endforeach ?>
</div>
</div>
I will answer you question generally.
If you want to call something in the view you have many options like you have the variable already calculated in advance and already passed it to view waiting for a call, or you can click on a link that maybe reload the current view with the new variable done, or you can use ajax to go get the variable from the controller without reloading.
So if the variable has fixed value that doesn't depend on your action, go with the first option or else chose one of the others.
On the Homepage I have called the content of Custpm Post Type with Ajax with this code
jQuery(document).ready(function($){
$.ajaxSetup({cache:false});
$(".sector_item a").click(function(){
var post_link = $(this).attr("href");
$(".sector_item_content").html("<div class='load' style='position:relative;margin-top:50px;padding-bottom:50px;'><img src='<?php bloginfo("template_url"); ?>/images/ajax-loader.gif'></div>");
$(".sector_item_content").load(post_link);
return false;
});
});
But In the single Page of this same Post Type I need to have another design with content so how I can detect the single page. I tried with Wordpess is_single() function but it doesnt work it displayed anyway in ajax.How Can I fix this?
Here is my single template.php
<?php if(is_singular('sector' )){
echo 'Single page' ;
}else{
while (have_posts() ) : the_post(); ?>
<div class="container single_page_inner">
<div class="row clearfix">
<div class="col-md-10 column" style="float:none;margin:auto;">
<div class="sector_single_title">
<?php the_title();?>
</div>
<div class="single_sector_contentmain"> <?php the_content();?></div>
<div class="single_sector_more">
<img src="<?php bloginfo(template_url);?>/images/single_secormoreline.png"/>
<div class="single_sector_button">
<span class="single_sec_leftline"></span>
<span class="single_sec_rightline"></span>
Click Here To Read More
<div class="more_line"></div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
If you want to be able to do different things when your single.php files is loaded from Ajax, you need to tell it somehow when it is getting an Ajax request.
One approach would be to add a parameter to your call, like this:
var post_link = $(this).attr("href") + '?isajax=1';
Then in your single.php file, test for this parameter:
<?php if(!empty($_GET['isajax'])){
// This is Ajax - display in Ajax format
} else {
// This is not Ajax - display in standard single.php format
}
use this:
is_singular('YOUR_POST_TYPE' );
CODEX
I'm new at this and some help will be apprciated.
I'm building a sidebar in a CI-Boilerplate-Project which contains modules (widgets) that i got run with HMVC https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc.
In the sidebar i have a widget that display a friendslist with status online/offline.
The user has the ability to switch the widgets on/off in the managementsection.
In the Profileview:
<aside class="sidebox right">
<?php foreach ($boxes as $boxName => $boxSetting)
{
echo Modules::run($boxName, $boxSetting['box_visible']);
}
?>
</aside>
if box_visible == 1 the widget will be displayed.
Controller:
class Myfriends extends SM_Controller
{
function __construct()
{
parent::__construct();
}
public function index($visible = false)
{
$user = $this->session->userdata('user');
$myf = $this->widget_model->get_friends($user['user_id'], 5);
$data['friends'] = $myf;
if ($visible) $this->load->view('myfriends', $data);
}
}
View:
<html>
<head>
<meta http-equiv="refresh" content="5">
</head>
<body>
<div class="box friendsbox">
<div id="header"><h3><?=$boxTitle?></h3></div>
<div id="boxcontent">
<ul>
<?php foreach ($friends as $friend): ?>
<li>
<div id="thb_img">
<img src="<?=img_thumb($friend['file_path'], 50, 50) ?>" />
</div>
<div id="short_desc">
<a href="<?= site_url('widget_functions/show_user/' . $friend['uu_id']) ?>">
<?= ucfirst($friend['user_name']) . ' ' . ucfirst($friend['user_lastname']) . ' ' ?>
</a>
<?php if ($friend['is_online']): ?>
<span style="color: green">online</span>
<?php endif; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
<div id="footer">ยป mehr</div>
</div>
</body>
</html>
Now, i need to update the friendslist every 1-2 min so i tryed to load the moduleview within an iframe:
<aside class="sidebox right">
<?php foreach ($boxes as $boxName => $boxSetting): ?>
<?php if ($boxName == 'myfriends' && $boxSetting['box_visible'] == 1) { ?>
<iframe src="<?php echo site_url('myfriends/index'); ?>" ></iframe>
<?php
}
else
{
echo Modules::run($boxName, $boxSetting['box_visible']);
}
?>
<?php endforeach; ?>
</aside>
BUT this dose not work! The place of the widget is emtpy.
Do you have any idea how to get that to work?
appreciate your help
I believe the main issue is with the way you initialize the index method. the index method is kinda tricky with parameters in Codeigniter. In my projects, the only way to get the vlues of arguments passed to the index parameters is by using the URI library method $this->uri->segment(n). In other words, I believe that the value of $visible is not properly passing to the index() body
Anyway, I think you should create another method in your MyFriends Class called render() for example, and call it instead of relaying on the index() method. now render() can play nicely with the $visible=false initialization trick.
Hope this helps
I'm calling an ajax function for user login.
After successful login, I want to refresh a div with content like (Ex. Welcome Mr. User, logout).
HTML:
<ul id="signup">
<li>Login</li>
<li>Signup</li>
</ul>
<ul id="loginbox">
<li><label>Enter email address</label><br/><input type="text" class="bookinginput" id="uemail"></li>
<li><label>Enter password</label><br/><input type="password" class="bookinginput" style="width: 151px;" id="upassword">
<button class="styled-button-8" style="margin-top: -43px;margin-left: 10px;" onClick="ulogin()">Login</button>
</li>
</ul>
Ajax:
function ulogin()
{
var uemail = $('#uemail').val();
var upassword = $('#upassword').val();
$.ajax({
type: "POST",
url: "udoologin.php",
data: {email:uemail, password:upassword}
}).done(function( result ) {
$("#loginbox").html(result);
});
}
After successful login, I'm able to change div#loginbox with successfull login message, but I want to replace another div#signup also with
<ul id="signup">
<li>Welcome <?php echo $user?></li>
<li>logout</li>
</ul>
How can I achieve this?
In PHP when u are printing the result,echo
<li>Welcome <?php echo $user?></li>
<li>logout</li>
joined with a seperator like '|||' or something.
in jquery
done(function( result ) {
var log=result.split('|||');
$("#loginbox").html(log[0]);
$('#signup').html(log[1]);
});
i'm in the middle of creating a website and i want to creat a dynamic dropdown menu.The problem is that when i load the entries from the database only the first entry is displayed correctly , the other entries in the menu are displayed as code. I'm using Xamp with PHP 5.3 and HeidiSql.This is my code :
<div id="1" class="mega solidblocktheme">
<div class="column">
<ul>
<li><a href="<?php include('../conect.php'); $result=mysql_query("SELECT * FROM tours");
while($data=mysql_fetch_row($result))
{ echo $data[2]; ?>"> <?php echo $data[1];
echo "</a></li>"; }
mysql_close($con);?>
</ul>
</div>
</div>
Your loop seems to be in the wrong place. Try something like this:
<ul>
<?php include('../conect.php');
$result=mysql_query("SELECT * FROM tours");
while($data=mysql_fetch_row($result))
{
echo '<li><a href="'.$data[2].'">'.$data[1];
echo "</a></li>";
}
mysql_close($con);?>
</ul>
You should not do it this way .
while($data=mysql_fetch_row($result))
{ echo $data[2]; ?>">
echo ""; }
For every loop , you are just adding content and close tags </a> and </li>
But you are not adding start tags for them .
What you can do is , first store your data result in a string
include('../conect.php');
$result=mysql_query("SELECT * FROM tours");
$data=mysql_fetch_row($result);
mysql_close($con);
$list = "";
foreach($data as $data_key) {
$list.= '<li> '.$data[1].' </li>';
}
So now you got your list
After that comes your HTML , just echo your built string
<div id="1" class="mega solidblocktheme">
<div class="column">
<ul>
<?php
echo $list;
?>
</ul>
</div>
</div>