Use of joomla methods stops file to work in plugin - ajax

I've created simple ajax voting plugin, it works fine.. till i start to use joomla methods in conf.php file. The file below is conf.php with simple php db query, if i put here for example $dbb = JRequest::getDbo(); it stops working .. or any other joomla methods. I can't get it what is wrong here?
$.ajax
({
type: "POST",
url: "plugins/system/ratingx/conf.php",
data: dataString,
cache: false,
success: function(html)
{
conf.php:
<?php
define( '_JEXEC', 1) or die;
defined( '_JEXEC' ) or die;
$mysql_hostname = "localhost";
$mysql_user = "px";
$mysql_password = "px";
$mysql_database = "jum";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
if($_POST['id'])
{
$id=mysql_real_escape_string($_POST['id']);
$name=mysql_real_escape_string($_POST['name']);
mysql_query("update messages set $name=$name+1 where id='$id'");
$result=mysql_query("select up,down from messages where id='$id'");
$row=mysql_fetch_array($result);
$up_value=$row['up'];
$down_value=$row['down'];
$total=$up_value+$down_value;
$up_per=($up_value*100)/$total;
$down_per=($down_value*100)/$total;
?>
<div style="margin-bottom:10px">
<b>Ratings for this blog</b> ( <?php echo $total; ?> total)
</div>
<table width="700px">
<tr>
<td width="30px"></td>
<td width="60px"><?php echo $up_value; ?></td>
<td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td>
</tr>
<tr>
<td width="30px"></td>
<td width="60px"><?php echo $down_value; ?></td>
<td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td>
</tr>
</table>
<?php
}

Not sure what version of Joomla! you're using but...
The problem is that you're calling a file that exists "outside" the Joomla! framework, just because you've defined _JEXEC doesn't mean the framework is loaded.
To be able to use JRequest or even JFactory::getDBO() you will also need to initialise Joomla!'s front-end app and load the framework/classes required. Doing that can potentially leave your site with a security issue.
I would suggest you create a basic component with an appropriate AJAX/RAW controller that your plugin can call through the default Joomla! path (i.e. index.php).
If you do want to by pass the Joomla! application then look at the main index.php for your version of Joomla to give you a guide has to how to load the applications framework.
p.s. JRequest::getDBO() isn't valid, getDBO() is not a method of JRequest (any version)

Related

Adding a Logon message to pages

I'm looking to create a log in feature every time users go to various members only pages, which returns them to the original page after logging in. I've seen various answers to this question but none of them seem to include a check feature followed by a return to the original page. At the moment the code I have created doesn't seem to recognize that I have logged in and keeps returning me to the log in form. Any answers will be greatly appreciated. I realize I am using deprecated code but that is the only version my host provider's servers recognize.
Here's the code I am putting at the top of each members page
<?php
session_start();
if($_SESSION['login'] != "yes" )
{
header("Location: main_login.php");
exit();
}
?>
This then opens the main_login.php page
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
On clicking the login button the following code in checklogin.php checks the entries
<?php
$host='.....'; // Host name
$username='.....'; // Mysql username
$password='........'; // Mysql password
$db_name='....'; // Database name
$tbl_name='......'; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_start();
$_SESSION['myusername'];
$_SESSION['mypassword'];
header("location:entry_form_european_languages.php");
}
else {
echo "Wrong Username or Password";
}
?>
The last header location refers to the page I would like to return to, which seems to the repeat the process of opening up the login and check in files- as if to indicate that the return page doesn't recognize that the log in was successful.
I would need to add something that was relative to each page, but since I don't know where I am going wrong with the fixed page return, I can't move on to that stage of coding.
I did have an alternative header address which took it to the following page login_success.php which gave the impression that username entries had been accepted, but this doesn't allow me to return to the original page
<?php
session_start();
if(isset($_SESSION[$myusername])){
header("Location:entry_form_european_languages.php");
}
?>
<?php
include '........';//Formatting for the page
?>
<html>
<body>
Login Successful
</body>
</html>
Thanks in advance.
There are a few extra things I needed to add to make the session details work, as follows. This code needs to go ahead of any other code on the page including any html code that relates to character formatting. Although I have coded it out the error reporting line is handy for indicating which line is not being read by the php server, should you have continued problems.
<?php
//error_reporting(E_ALL); ini_set('display_errors', 'On');
session_start();
ob_start();
if(!isset($_SESSION['myusername'])){
header('Location:main_login.php');
}
else if (isset($_SESSION['myusername'])){
}
$myusername=$_SESSION['myusername'];
$Page_Title ='Members Profile';
?>

Adding product description , product image etc in custom Transactional Email for New order

I have created a new custom template using the New order default template in my Magento admin panel.
There are some default variables available to be inserted into the email. But I want to add some other variables like product description into the email template.
In my email after order creation , I want to send description and other details of all products that are present in my cart.
What I have in template is :
<table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
<tr>
<td valign="top">
<img src="xyz.jpg"/>
</td>
</tr>
<tr>
<td>
<?php
$order = Mage::getModel('sales/order')->loadByIncrementId("{{var order.increment_id}}");
$items = $order->getAllVisibleItems();
foreach($items as $i):
echo $i->getProductId();
echo $i->getDescription();
echo $i->getDescription();
endforeach;
?>
</td>
</tr>
I want to achieve something like this. Is it possible adding PHP code into the email template and get the variables working. Or I have to define this php code somewhere else? If yes, can you please specify where to make these changes?
Thanks
I dont think u need to load product object in email template it is already loaded if not u can do that but sure u dont need sales order object that sure.
app\design\frontend\default\imsov2\template\email\order\items\order\default.phtml
<?php echo $this->escapeHtml($_item->getDescription()) ?>
and so on...

Ajax page fails with undefined variable error message

I'm just trying to write my first ajax enabled page. Needless to say, I've run into a problem. And I think it reveals a fundamental misunderstanding of how ajax is supposed to work.
Here's a description of what i'm trying to accomplish.
I have a page with a table containing records from my database. When the user clicks on my refresh button, i want to requery the database for all records and display them without refreshing the page.
Here's my controller:
class Dashboard extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('date');
$this->load->helper('url');
}
public function index()
{
$this->load->model('locations_model');
$emess = '';
$data['clienthistory'] = $this->locations_model->get_locations();
$data['main_content'] = 'dashboard';
$this->load->view('includes/template', $data);
$this->output->enable_profiler(TRUE);
}
public function index2()
{
echo('inside the getlatest method');
$this->load->model('locations_model');
$data['clienthistory'] = $this->locations_model->get_locations();
//build HTML table to send back to view
$data['latestdashboardHTML']= "<table class='table table-bordered table-striped'><thead>";
$data['latestdashboardHTML']=$data['latestdashboardHTML'] & "<tr><th>IP</th><th>Name</th><th>Last Updated</th></tr></thead><tbody>" ;
foreach ($data['clienthistory'] as $histitem)
{
$data['latestdashboardHTML'] = $data['latestdashboardHTML'] & "<tr>";
$data['latestdashboardHTML'] = $data['latestdashboardHTML'] & "<td>" & $histitem['network'] & "</td>";
$data['latestdashboardHTML'] = $data['latestdashboardHTML'] & "<td>" & $histitem['name'] & "</td>";
$data['latestdashboardHTML'] = $data['latestdashboardHTML'] & "<td>" & $histitem['lastupdated'] & "</td>";
$data['latestdashboardHTML'] = $data['latestdashboardHTML'] & "</tr>";
}
$data['latestdashboardHTML'] = $data['latestdashboardHTML'] & "</tbody></table>";
$data['main_content'] = 'dashboard';
echo ($data['latestdashboardHTML']) ;
$this->load->view($data['main_content'] );
}
}
And here's the code in my view:
<h2>Client Dashboard</h2>
<br/><p>
<?php echo form_open('Dashboard/index');
echo form_submit('submit', 'Refresh Data', 'id="submit" class="btn-primary"');
?>
</p>
<div class="row-fluid">
<div class="span12" id="ajaxcontainer">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>IP</th>
<th>Name</th>
<th>Last Updated</th>
</tr>
</thead>
<tbody>
<?php foreach ($clienthistory as $histitem): ?>
<tr>
<td><?php echo $histitem['network'] ?></td>
<td><?php echo $histitem['name'] ?></td>
<td><?php echo $histitem['lastupdated'] ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
</div><!--/row-->
<?php
echo form_close();
?>
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../assets/js/jquery.js"></script>
<script src="../assets/js/bs-transition.js"></script>
<script src="../assets/js/bs-alert.js"></script>
<script src="../assets/js/bs-modal.js"></script>
<script src="../assets/js/bs-dropdown.js"></script>
<script src="../assets/js/bs-scrollspy.js"></script>
<script src="../assets/js/bs-tab.js"></script>
<script src="../assets/js/bs-tooltip.js"></script>
<script src="../assets/js/bs-popover.js"></script>
<script src="../assets/js/bs-button.js"></script>
<script src="../assets/js/bs-collapse.js"></script>
<script src="../assets/js/bs-carousel.js"></script>
<script src="../assets/js/bs-typeahead.js"></script>
<script type="text/javascript">
$('#submit').click(function() {
alert('here');
$.ajax({
url:"<?php echo site_url('Dashboard/index2'); ?>",
type: 'POST',
success: function(msg) {
alert(msg);
$('#ajaxcontainer').replaceWith(msg);
}
});
return false;
});
</script>
Problem:
the first time through, the page loads properly. all records show up with the correct values. But when i try the refresh button, the javascript executes, calls the index2 method.. but then it fails in the view on line 22 - which is where I'm trying to loop through the clienthistory array.
The error message is: Undefined variable: clienthistory.
SO here's my question. I thought the way ajax worked was that it only updated a section of the page. So I guess I don't understand why it's "redo-ing" this part of the view.
Can someone explain what I'm doing wrong, and more importantly, if my understanding of ajax is correct. I'd also prefer it if i could combine the two methods into one... i tried but had some problems so I ended up just copying index() to index2() and just working with that.
The other behavior that I'm noticing is that the title "Client Dashboard" as well as the button is showing up twice when execute the ajax method.
Thanks.
I found my problem. In index2() i'm loading the view again.
i shouldn't reload the view. i just need to pass the html string to the caller.
this in part resolves my problem.
so specifically in my controller i replaced:
$this->load->view($data['main_content'] );
with
return $htmlstring;
where $htmlstring contains the same string as $data['latestdashboardHTML']
the other problem is that i needed to specify the dataType that i was expected back from the controller.
so i added the following line to my .ajax function:
dataType: 'html'
Finally, I read online somewhere that it's not a good idea to have the controller create any HTML. this is supposed to be the job of the view.
i guess i will try to pass an array from controller to view. and then have the jquery code recreate the contents of my div.
thanks

Problem with ajax using symfony 1.4

I have a very strange problem when I using ajax in symfony 1.4. I've used the jobeet example (day 18) but it doesn't work
This is my indexSuccess.php
<script type="text/javascript" >
$(document).ready(function(){
$('#buscador').keyup(function(key)
{
if (this.value.length >= 3 || this.value == '')
{
$('#per').load( $(this).parents('form').attr('action'),
{ query: this.value + '*' });
}
});
});
</script>
<h1>Lista de personas</h1>
<p>Busque o cree registros de personas en el sistema (Estudiantes, funcionarios, docentes).</p>
<form action="<?php echo url_for('personas/index')?>">
<table>
<tr>
<td><input type="text" name="buscar" id="buscador"/></td>
<td><img src="/images/iconos/Search.png"/></td>
</tr>
</table>
</form>
<p style="font-size: 11px;color: gray;">Digite un nombre, apellido o número de identificación para buscar</p>
<div class="per" id="per">
<?php echo include_partial('personas/buscaPersonas',array('personass'=>$personass)); ?>
</div>
The jquery script detects characters in the input, when I write 3 or more characters it should load the div with id='per'. Here is my personasAction.class.php
public function executeIndex(sfWebRequest $request)
{
$this->personass = array();
if($request->isXmlHttpRequest())
{
$this->personass = $this->getRoute()->getObjects();
return $this->renderPartial('personas/buscaPersonas', array('personass'=> $personass));
}
}
When I load the page I dont want to see any result. So, when I do a ajax call, I should reload the partial "_buscaPersonas.php" with all results (just for try), but I load the _form.php partial.
This is my partial:
<table>
<?php foreach($personass as $personas): ?>
<tr>
<td colspan="5" class="tituloTD"><?php echo $personas->getNombre(); ?></td>
</tr>
<tr>
<th>Numero identificación: </th><td><?php echo $personas->getNumeroid() ?></td>
<th>Email: </th><td><?php echo $personas->getEmail(); ?></td>
<td> <img src="/images/iconos/editar.png"/></td>
</tr>
<?php endforeach; ?>
</table>
I've trying to find where is the problem but I not get it. When I use the button for normal search it works, load the correct partial but whit ajax load other partial.
Please somebody knows what is my error.
thanks
I think this due to the path of your load ajax function:
$('#per').load( $(this).parents('form').attr('action'),
{ query: this.value + '*' });
$(this).parents('form').attr('action') is certainly a wrong value.
Try this :
url_for('personas/index')
Finally I find the error. I dont know what happen but if I send data with the load function I have and error. So I had to send the data using the url, and is works:)
if (this.value.length >= 3 || this.value == '')
{
$('#per').load( "<?php echo url_for('personas/index')?>"+"?query="+this.value);
}

how to use Ajax in this code?

</tr>
<tr>
<td><input type="radio" name="ans" id='ans_2' value="2" /> <? echo " "; echo $ans2 = $ques['ans_2'];?></td>
</tr>
<tr></tr>
<tr>
<td><input type="radio" name="ans" id='ans_3' value="3" /> <? echo " "; echo $ans3 = $ques['ans_3'];?></td>
</tr>
<tr></tr>
<tr>
<td><input type="radio" name="ans" id='ans_4' value="4" /> <? echo " "; echo $ans4 = $ques['ans_4'];?></td>
</tr>
hello everyone i am creating online MSQ's website and i need your help in this code
when user click on the radio button the given answer will store in db how can i do this?
Firstly, I strongly suggest you use a JS Framework like jQuery to do it. This is how you would do this in jQuery:
$('input.radio').click(function() {
$.ajax({
url: 'script.php?answer=' + $(this).attr('value'),
success: function(data) {
// this is the server response
}
});
});
Of course your script.php file should manage the DB connection and everything else. You may need to add extra parameters but this is a general solution that you can use in cases similar to this one.
EDIT
I used the 'input.radio' selector since you didn't add any specific class to the radio buttons, of course this will apply that click function to every radio button on the page. You may need to add a more specific selector (like a class selector) to affect only the correct buttons
$('input.radio[name='ans']).click(function() {
$.ajax({
url: 'script.php?answer=' + this.value,
success: function(data) {
// this is the server response
}
});
});
Pretty much the same as above. Except you can spercify this group of inputs by name:
$('input.radio[name='ans']).click // binds the ajax function to the click event
And you can just use this.value instead of:
$(this).attr('value')
I would recommend using jQuery also

Resources