I'm trying to create a session variable joomla style with ajax when checkboxes are selected. Here is my code in the select_thumb.ajax.php file:
$_SESSION['ss'] = $value;
$response = $_SESSION['ss'];
echo $response;
}
exit;
// Get db connection
$db = JFactory::getDbo();
//create new query object
$query = $db->getQuery(true);
//Prepare insert query
$query
->insert($db->valueChbx('download_variable'))
// Set the query using populated query object and execute it.
$db->setQuery($query);
$db->execute();
?>
Here is my HTML for the checkboxes:
<input type="checkbox" id="thumbselect" name="valueChbx" class="checkbox" value="/import/images/'+data[i]['filename']+'">';
I haven't coded the ajax through javascript yet because I'm wondering if i should use onFocus? There could be multiple checkboxes selected. Thanks for any help in advance.
Do not use PHP's default session variable in Joomla application use its native factory for that.
Set a session variable
$session = JFactory::getSession();
$session->set('name', "value");
Get a session variable
$session = JFactory::getSession();
echo $session->get('name');
more .
hope it helps..
Related
I have an HTML login form and when a user logs in I want to redirect that user to another page and change some DOM's on it.This redirection is done with a PHP script that verifies the user and password.
I have this:
<?php
set_time_limit(0);
header('content-type: text/html; charset=utf-8');
header("access-control-allow-origin: *");
include 'connect.php';
$pass= $_POST['pass'];
$sql = "SELECT password FROM users WHERE password ='$pass'";
$result = mysql_query($sql);
$row=mysql_fetch_array($result);
if($row[0]!='') {
$query = mysql_query("SELECT user FROM users WHERE password ='$pass'");
$user = mysql_fetch_array($query, MYSQL_ASSOC);
$user=$user["user"];
echo "<script>window.location = 'http://localhost/public_html/as/index.php?pass=$pass';document.getElementById('log').innerHTML = 'LOGGED WITH: $user';$('#login-form').slideUp();document.getElementById('logout').style.display='';$('#log').unbind('click');</script>";
} else {
echo "<script>document.getElementById('text').value = ''; document.getElementById('text').placeholder = 'Type a valid key';</script>";
}
?>
This is just to learn how to deal with login-forms, so don't mind about the security thing.
My problem is here:
echo "<script>window.location = 'http://localhost/public_html/as/index.php?pass=$pass';document.getElementById('log').innerHTML = 'LOGGED WITH: $user';$('#login-form').slideUp();document.getElementById('logout').style.display='';$('#log').unbind('click');</script>";
This code redirects to the window.location page but doesn't changes the other DOM's in HTML page.
How can I fix this?
Thanks in advance!
you cant use that link. you have 2 solution for this problem.
use SESSION[] array in php to define user is loged in and in target page , check if your variable in session array was set, echo your javascript on page to do what you want.
use query string to tell target page that user logged in and with php in target page, echo your javascript code.
i think you have better solution for your problem. but in order to your request, that's my offer. :)
EDITED:
in first line of your php file use:
session_start();
when your username and password was pass, set session array like this:
$_SESSION["userName"]=$username;
and the end of php script use it like this:
if(isset($_SESSION["userName"]))
echo "<script>....your code that you want run when user login...</script>";
This is page of wordpress I am adding core php code for displaying data into table as per select input for that I am using javascript for gettin input and pass to the page page.And that page getting value as per value fir a mysql query.But my core php code dispaly as it is on screen. I am not able to understand how to do this. Beacuse I am new in wordpress Today is my first day in wordpress. Please help me ..Thanks in advance
<?php
$var=$_COOKIE['v'];
$id = explode(",", $var);
echo 'hawno:'.$id[1];
$conn = mysql_connect("localhost", "root", "");
$db = mysql_select_db("shepherddb");
$err = error_reporting(E_ALL && ~E_NOTICE);
$result=mysql_query("select ship_id,track_id,track_ship_id,track_mod_of_transport,track_location,track_status from
tracking,shipment");
while($data = mysql_fetch_array($result)) {
print_r($data);
}
?>
You need to use $wpdb global object provided by wordpress.
For example ,
global $wpdb;
$results = $wpdb->get_results( 'SELECT * FROM wp_options WHERE option_id = 1', OBJECT );
You can also use pre_get_posts action to modify the POSTS Query.
add_action( 'pre_get_posts', 'your_theme_function' );
Let me know if you need more information on the same.
I'm trying to get PHP code like this to work:
<?php
$hostname = '******';
$database = 'firstdb';
$username = '*****';
$password = '*****';
$dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$sortvalue = "datbase_percent";
$sortorder = "ASC";
$sql = "select * from advanced_data where category like age_group order by {$sortvalue} {$sortorder};";
$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
header('Content-type: application/json');
echo json_encode($result);
?>
What I want to be able to do is define the $sortvalue and $sortorder via an AJAX call. I'm getting it now like this:
$.getJSON('all_get_2.php', function(data) {
Two questions...my PHP code doesn't work because I can't figure out the proper syntax for building the $sql variable the makes up the mySQL query. I've tried a bunch of things, but keep getting 500 errors. If I just type in the values, then the code works, so I know it's just a problem with my syntax.
Second question...what's the best way to pass a variable into $sortvalue and $sortorder from my front end? I know it's something using $.ajax, but not sure of the best way to do it. The idea is that the user would click a button, which corresponds with sorting a chart ascending or descending and reloads the chart without reloading the page. Any direction here would be appreciated.
I've added a table in my mySQL database which has two variables: ID and a URL.
ID has the value 1
and
URL has http://www.examplesite.com
I want to get "www examplesite com" from the database into a PHP file.
The PHP file should then send this string to a javascript file which will then open that URL.
So far I've been using getJSON with little success.
I'm new to PHP and Java and would really appreciate some help!
I want something like this in my .js file
$.getJSON('getlink.php', {'link'}, function(e) {
alert('Result from PHP: ' + e.result);
});
window.open(linkVariable'_blank')
I would like linkVariable to be www examplesite com
The javascript is linked to another php file which has a clickable element for the window.open.
How can I get the getlink.php and the .js file to communicate with each other?
EDIT:
My getlink.php would look something like this without any echo. The connection to mySQL is already written.
function get_links($url_link) {
$sql = "SELECT `name` FROM `variables` WHERE ID=?";
$res = $this->db->query($sql, array($url_link))->row_array();
return $res['URL'];
it would be better if you posted what code is in getlink.php.
I'd suggest you proceed like this. In getlink.php query the database to return the correct row.
There is different ways to do that, depending on what framework or library (PDO,mysql_,) you are using.
Then return the result as json
In the same file, call the function you just defined.
It should look to something like this
<?php
function get_links($url_link) {
$sql = "SELECT `name` FROM `variables` WHERE ID=?";
$res = $this->db->query($sql, array($url_link))->row_array();
return $res;
}
$result = get_links(1); //whatever the id is
return json_encode($result);
?>
In your javascrit you can do something like this
$.get(
"getlink.php",
function(data) {
alert(data.URL);
}
);
//My Idea
Read URL from the database and give it to the variable $link code below
$result=mysqli_query($dbconnection, "SELECT * FROM url"); //You can specify conditions
while($row=mysqli_fetch_array($result)){
$link=$row["URL"]; //We have our url from database here
}
//Link between JAVASCRIPT AND PHP
Put this code in a php to take $link and give it to getlink() function as a parameter
<script>
getlink("<?php echo $link?>"); //A java script function from your JS file to get url
</script>
It appears that json_encode is being VERY picky about what other stuff can be inside my PHP file. Which is fine, because I just do what I normally would do in file A (with json_encode) in it's own file.
I just thought I would ask because I am storing a variable in the $_SESSION instead of updating my database with the variable because json_encode doesn't seem to want to work when I have all of the code in its file.
For instance, this code doesn't work:
<?php
session_start();
include 'dbcon.php';
$sessionID = uniqid();
echo json_encode($sessionID);
if(isSet($_POST['clearSession']) == '1')
{
$query = "UPDATE currentID SET id=('0')";
$execute = $mysqli->query($query) or die($mysqli->error.__LINE__);
} else {
$query = "UPDATE currentID SET id=('$sessionID')";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
}
?>
When going to the file in my browser, I do in fact get the json_encode results, however when my Javascript calls it it doesn't seem to correctly import it.
So, for now I simply have two PHP files:
<?php
session_start();
$sessionID = uniqid();
$_SESSION["sessionID"] = $sessionID;
echo json_encode($sessionID);
?>
Which echo's the same thing as in the first file, but this time my JavaScript correctly imports it.
and
<?php
session_start();
include 'dbcon.php';
if(isSet($_POST['clearSession']) == '1')
{
$query = "UPDATE currentID SET id=('0')";
$execute = $mysqli->query($query) or die($mysqli->error.__LINE__);
} else {
$sessionID = $_SESSION["sessionID"];
$query = "UPDATE currentID SET id=('$sessionID')";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
}
?>
I guess my question is, why does this happen? It seems kind of silly that I have to store the uniqid in a SESSION so that my other PHP file can add it to the database. Whereas if I simply had it in one file, then I could just update the database when I generate a new uniqid and avoid having to use $_SESSION in the first place.
You have this:
$sessionID = uniqid();
echo json_encode($sessionID); // "53d4c17abfe87"
Since uniqid() produces a plain string, your output is not valid JSON as per the format specification. You'll need something like this instead:
$sessionID = uniqid();
echo json_encode(array($sessionID)); // ["53d4c17abfe87"]
Why does json_encode() generate invalid JSON in the first place? Because some times it's useful to generate partial JSON. For instance, it's a handy trick to inject values into generated JavaScript code:
var foo = <?=json_encode($sessionID)?>;
It's also documented:
PHP implements a superset of JSON - it will also encode and decode
scalar types and NULL. The JSON standard only supports these values
when they are nested inside an array or an object.
So to answer the question title:
Is json_encode extremely picky?
On the contrary, it's fairly relaxed!
You don't need to store it in a session necessarily, it's the fact that $_SESSION is an array.
So, what you would want would be something like this:
echo json_encode(array('sessionID' => $sessionID));
And then when you parse the JSON with JavaScript you can access it like this:
obj = JSON.parse(jsonObj);
alert(obj.sessionID);
Obviously, jsonObj is the JSON passed from the server.
Hope this helps!