i want the value of $code variable to get on my other page--- captcha.php.
captcha_image.php
$captcha = new CaptchaCode(); //class defined in captcha_code.php
$code = str_encrypt($captcha->generateCode(6)); //function defined in captcha_code.php
$captcha1 = new CaptchaImages();
$captcha1-> GenerateImage($width,$height,str_decrypt($code));
captcha.php
<img style="cursor: pointer;width: 50px;height: 50px;" src="refresh.png" onclick="refresh_captcha();"/>
<input type="hidden" name="security_check" value="<?php echo $code; ?>"> // want value of $code here
<script type="text/javascript">
function refresh_captcha()
{
var img = document.getElementById('captcha_img');
img.src = 'captcha_images.php';
jQuery("#captcha_img").attr("src",img.src);
}
</script>
I cant include captcha_images.php file in my code and even dont want it to be done using sessions, tried that way.If anyone has a solution for this, please help me to solve this issue.
Better solution is save code into SESSION.
For example:
captcha_image.php:
session_start();
$captcha = new CaptchaCode();
$code = $captcha->generateCode(6);
$captcha1 = new CaptchaImages();
$captcha1-> GenerateImage($width,$height,$code);
$_SESSION["captchacode"] = $code;
And check correctness after submit of the form:
session_start();
...
if($_SESSION["captchacode"]!=$_POST["security_check"]){
echo "Wrong captcha!";
}else{
// captcha is correct, process the form
}
If you cannot use cookies and session, you cannot get information from captcha_image.php which returns only image. You must generate information in else request, for example:
<img id="captcha_img" src="captcha_images.php?encoded_code=<?php echo $code ?>" onclick="refresh_captcha();"/>
<input type="hidden" id="captcha_hidden" name="security_check" value="<?php echo $code ?>">
<script type="text/javascript">
function refresh_captcha()
{
// generate_captcha.php returns only encoded captcha
$.get('generate_captcha.php', function(encoded_code) {
$('#captcha_hidden').val(encoded_code);
$('#captcha_img').attr("src","captcha_images.php?encoded_code="+encoded_code);
});
}
</script>
Here generate_captcha.php returns encoded captcha, captcha_images.php doesnt generate code, only decode code from hims parameter encoded_code and this code is also inserted into hidden.
Related
I want to open a new page and transfer data from an array. Using the name of the image seemed like the easiest way so that's what i want to do.
on the page i want to call it
function meghiv($img)
{
$be=$img.alt;
echo $be;
session_start();
$_SESSION['kod'] = $be;
}
for($j=0;$j<4;$j++)
{
echo ' <img src="'.$nevek[$i].'.png" class="card-img-top " alt="'.$i.'" onclick="meghiv(this)"> ';
$i++;
}
on the new page
<?php
session_start();
echo $_SESSION['kod'];
?>
I don't know if it answers your question but try using javascript to load the image name into your php file
let images = document.querySelectorAll('.card-img-top'); // returns NodeList
let img_list = [...images]; // converts NodeList to Array
img_list.forEach(div => {
div.addEventListener("click", function(e){
e.preventDefault()
let alt = div.getAttribute("alt")
window.open(`https://link.here/?alt=${alt}`, "_blank");
})
});
Then in your php file
$image_name = $_GET['alt'];
I use the code below for simple search and it works well in index.php but because i added the code in header.html for include in all pages and the code result work only in index.php.
https://stackoverflow.com/a/34131517/5227465
index.php?text=keyword = ok
otherpage.php?text=keyword = here not work because search Processing only in index.php
I think the problem in this element means the current page that contains the code:
(document.getElementById)
<form id = "your_form" onsubmit="yourFunction()">
<input type="text" name="keywords">
<input type="submit" value="Search">
</form>
function yourFunction(){
var action_src = "http://localhost/test/" + document.getElementsByName("keywords")[0].value;
var your_form = document.getElementById('your_form');
your_form.action = action_src ;
}
any help?
I didnt test it but try this:
function yourFunction(){
var action_src = "http://localhost/test/<?php echo basename(__FILE__, ''); ?>" + document.getElementsByName("keywords")[0].value;
var your_form = document.getElementById('your_form');
your_form.action = action_src ;
}
Can't pass php session variable to javascript string variable
While the $_SESSION['Id'] variable exists, the javascript can't seem to bring it at least with this syntax:
CODE
<?php session_start(); ?>
<script>
var a = "<?php echo $_SESSION['Id']; ?>";
alert(a);
</script>
Your syntax looks fine. What happens if you write this?
<?php
php session_start();
echo '<div style="padding:30px; background-color:#ffffff;"><pre>'.print_r($_SESSION, true).'</pre></div>';
?>
<script>
var a = "<?php echo $_SESSION['Id']; ?>";
alert(a);
</script>
If that doesn't work then try manually setting the ID before the echo
<?php
php session_start();
$_SESSION['Id'] = 'AN ID!!!';
echo '<div style="padding:30px; background-color:#ffffff;"><pre>'.print_r($_SESSION, true).'</pre></div>';
?>
First, like the comments have mentioned, make sure you're using the correct case of id, whether it's id or Id.
Second, try using json_encode to convert it for javascript use. No need for "":
var a = <?php echo json_encode($_SESSION['Id']); ?>;
Try this to see if the variable $_SESSION['Id'] exists and is set to something
<?php
session_start();
print_r( $_SESSION );
?>
<script type="text/javascript">
var a = "<?php echo $_SESSION['Id']; ?>";
alert(a);
</script>
My PHP code is:
<?php
class Sample{
public $name = "N3mo";
public $answer = "";
}
if( isset( $_GET['request'] ) ){
echo "Starting to read ";
$req = $_GET[ 'request' ];
$result = json_decode($req);
if( $result->request == "Sample" ){
$ans = new Sample();
$ans->answer = " It Is Working !!! ";
echo json_encode($ans);
}else{
echo "Not Supported";
}
}
?>
Is there anything wrong
I want to send a JSON to this php and read the JSON that it returns using java script , I can't figure out how to use JavaScript in this , because php creates an html file how Can I use $_getJson and functions like that to make this happen ?!
I tried using
$.getJSON('server.php',request={'request': 'Sample'}) )
but php can't read this input or it's wrong somehow
thank you
try this out. It uses jQuery to load contents output from a server URL
<!DOCTYPE html>
<html>
<head>
<title>AJAX Load Test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#button").click(function(event) {
$('#responce').load('php_code.php?request={"request":"Sample"}');
});
});
</script>
</head>
<body>
<p>Click on the button to load results from php_code.php:</p>
<div id="responce" style="background-color:yellow;padding:5px 15px">
Waiting...
</div>
<input type="button" id="button" value="Load Data" />
</body>
</html>
Code below is an amended version of your code. Store in a file called php_code.php, store in the same directory as the above and test away.
<?php
class Sample
{
public $name = "N3mo";
public $answer = "";
}
if( isset( $_GET['request'] ) )
{
echo "Starting to read ";
$req = $_GET['request'];
$result = json_decode($req);
if( isset($result->request) && $result->request == "Sample" )
{
$ans = new Sample();
$ans->answer = " It Is Working !!! ";
echo json_encode($ans);
}
else
{
echo "Not Supported";
}
}
Let me know how you get on
It would be as simple as:
$.getJSON('/path/to/php/server.php',
{request: JSON.stringify({request: 'Sample'})}).done(function (data) {
console.log(data);
});
You can either include this in <script> tags or in an included JavaScript file to use whenever you need it.
You're on the right path; PHP outputs a result and you use AJAX to get that result. When you view it in a browser, it'll naturally show you an HTML result due to your browser's interpretation of the JSON data.
To get that data into JavaScript, use jQuery.get():
$.get('output.html', function(data) {
var importedData = data;
console.log('Shiny daya: ' + importedData);
});
I have a PHP foreach loop which is getting an array of data. One particular array is a href. In my echo statement, I'm appending the particular href onto my next page like this:
echo 'Stats'
It redirects to my next page and I can get the URL by $_GET. Problem is I want to get the value after the # in the appended URL. For example, the URL on the next page looks like this:
stats.php?url=basket-planet.com/ru/results/ukraine/?date=2013-03-17#game-2919
What I want to do is to be able to get the #game-2919 in javascript or jQuery on the first page, append it to the URL and go to the stats.php page. Is this even possible? I know I can't get the value after # in PHP because it's not sent server side. Is there a workaround for this?
Here's what I'm thinking:
echo 'Stats';
<script type="text/javascript">
function stats(url){
var hash = window.location.hash.replace("#", "");
alert (hash);
}
But that's not working, I get no alert so I can't even try to AJAX and redirect to the next page. Thanks in advance.
Update: This is my entire index.php page.
<?php
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
foreach ($html->find('div[class=games] div[class=games-1] div[class=game]') as $games){
$stats = $games->children(5)->href;
echo '<table?
<tr><td>
Stats
</td></tr>
</table>';
}
?>
My stats.php page:
<?php include_once ('simple_html_dom.php');
$url = $_GET['url'];
//$hash = $_GET['hash'];
$html = file_get_html(''.$url.'');
$stats = $html->find('div[class=fullStats]', 3);
//$stats = $html->find('div[class='.$hash.']');
echo $stats;
?>
What I want to be able to do is add the hash to the URL that is passed on to stats.php. There isn't much code because I'm using Simple HTML DOM parser. I want to be able to use that hash from the stats.php URL to look through the URL which is passed. Hope that helps...
Use urlencode in PHP when you generate the HREFs so that the hash part doesn't get discarded by the browser when the user clicks the link:
index.php:
<?php
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
echo '<table>';
foreach ($html->find('div[class=games] div[class=games-1] div[class=game]') as $games){
$stats = $games->children(5)->href;
echo '<tr><td>
Stats
</td></tr>';
}
echo '</table>';
?>
Then on the second page, parse the hash part out of the url.
stats.php:
<?php
include_once ('simple_html_dom.php');
$url = $_GET['url'];
$parsed_url = parse_url($url);
$hash = $parsed_url['fragment'];
$html = file_get_html(''.$url.'');
//$stats = $html->find('div[class=fullStats]', 3);
$stats = $html->find('div[class='.$hash.']');
echo $stats;
?>
Is this what you're looking for?
function stats(url)
{
window.location.hash = url.substring(url.indexOf("#") + 1)
document.location.href = window.location
}
If your current URL is index.php#test and you call stats('test.php#index') it will redirect you to index.php#index.
Or if you want to add the current URL's hash to a custom URL:
function stats(url)
{
document.location.href = url + window.location.hash
}
If your current URL is index.php#test and you call stats('stats.php') it will redirect you to stats.php#test.
To your comment:
function stats(url)
{
var parts = url.split('#')
return parts[0] + (-1 === parts[0].indexOf('?') ? '?' : '&') + 'hash=' + parts[1]
}
// stats.php?hash=test
alert(stats('stats.php#test'))
// stats.php?example&hash=test
alert(stats('stats.php?example#test'))