Put JavaScript variable into PHP with AJAX? - biztalk-mapper

Here's a some of my JavaScript
function myFunction() {
var finalMove = "a1";
$.post("index.php", {postFinalMove:finalMove});
];
Here is my PHP code. The commented lines are just two of the things I've tried.
<?php
session_start();
$db = mysqli_connect("localhost","myUsername","myPassword","abraDB");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//if (isset($_POST['postFinalMove']))
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
//if ($_POST['postFinalMove'])
//{
// $turn = ($db, $_POST['postFinalMove']);
// echo $turn;
//}
if ($_POST['logout'])
{
session_start();
$_SESSION = array();
session_destroy();
}
mysqli_close($db);
?>
As soon as I uncomment some of the code I've tried, my entire webpage is blank with no errors and no source code, which makes this hard for me to debug. Javascript function fires just fine, its my index.php page that crashes. PHP code was working great until this. Thanks in advance

You should be able to do the following in your PHP script:
if(isset($_POST['postFinalMove'])){
echo $_POST['postFinalMove'];
}
This line is probably causing the error:
// $turn = ($db, $_POST['postFinalMove']);
That statement does not make any sense. What are you trying to store in $turn? If you want to store the $_POST['postFinalMove'] you would have:
$turn = $_POST['postFinalMove'];

Take a look at the GIF image and you will see for yourself..!
Replace your code with this line as and you are good to go then :
$turn = $_POST['postFinalMove'];
echo $turn;
It's just a simple Syntax Issue nothing more than that Dear..! :D

Related

laravel custom blade directive except tag function for htmlentites output

\Blade::directive('specialReplace', function($expression){
$expression = explode(',', $expression);
$exception = $expression[0];
$output = htmlspecialchars($expression[1]);
if ($exception == "img") {
$output = str_replace("<img", "<img", $output);
$output = str_replace("/>", "/>", $output);
} else {
$output = str_replace("<".$exception.">", "<".$exception.">",$output);
$output = str_replace("</".$exception.">", "</".$exception.">",$output);
}
return "<?PHP echo $output?>";
});
#specialReplace(img, <img src=....)
I try to make a custom function for html out image from database without htmlentites in laravel.
My problem is I get an error syntax error, unexpected '&' which I have no idea
anyone know how to fix this?
Try that, it should fix the error
return "<?PHP echo \"$output\"?>";
Also as an argument to your function, given the input
#specialReplace(img, <img src=....)
You'll receive an exact string you passed to the direcive (together with the brackets)
(img, <img src=....)
It doesn't look like you parse it properly.

DomPDF setting landscape orientation

I am having trouble setting DOMPDF to use landscape orientation. Could anyone shed some light on my problem in the code below?
function delivary_voucher($arr = null, $orientation = 'landscape') {
$this->load->library('dompdf_gen');
$this->load->library('email');
$this->load->model('Emailtemplatemodel');
I am having the same problem. After searching a lot I have finally understood how to solve the issue. I am answering this just after my coding. Hope it could help you and also for a reference of SO.
Here is how I solve the problem:
I am using dompdf as a helper, so I put the folder dompdf into system>helpers folder.
Then I have created a helper file named dompdf_helper.php into the same folder containing the following code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function get_pdf($html, $paper_size='a4', $orientation='portrait', $filename='', $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
// THE FOLLOWING LINE OF CODE IS YOUR CONCERN
$dompdf->set_paper($paper_size, $orientation);
$dompdf->render();
// added by Siddiqui Noor on 29 Nov, 15
if( !empty($filename) && $stream ) {
$dompdf->stream($filename.".pdf"); // to Download PDF
} else if ($stream) {
$dompdf->stream($filename.".pdf",array('Attachment'=>0));// to open in a window
} else {
return $dompdf->output();
}
}
?>
My system folder looks like the this
Finally I call the get_pdf function in my controller like:
$data = 'Your result set goes here';
$this->load->helper(array('dompdf', 'file'));
$html = $this->load->view($view, $data, true);
get_pdf($html,'a4', 'landscape');
Happy coding as code is poetry :)

PHP Sessions: Generate a variable and save it for session

I am totally new to php sessions. I am not able to get a simple task done.
This is what I am trying to do:
A visitor of my website gets shown a random image
well this part works so far. But everytime the user goes on to another page (I have this random image shown on all pages) the script generates a new random image to be shown.
What I want to do now is:
Save the random image variable to a session so he will see the same image on each page he visits while he has the session saved.
Here is my working code to get a random image without saving it into sessions. If anybody could help me with how the code should look like so it works with sessions would be awesome. Remember: I am a total newby when it comes to sessions.
As you can see I need the variable $img to be stored into a session after it got generated. And the script only to strt again on a new site visit if a user hasnt stored the $img variable in his session.
<?php
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = #opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed
$num = array_rand($ar);
return $ar[$num];
}
$root = '';
// use if specifying path from root
//$root = $_SERVER['DOCUMENT_ROOT'];
$path = 'images/';
// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
?>
<img src="/<?php echo $path . $img ?>" alt="image" />
You'll need to add this at the top of every page to get your image:
session_start();
if(isset($_SESSION['UserImg'])){
$img = $_SESSION['UserImg'];
}
else {
$img = getRandomFromArray($imgList);
$_SESSION['UserImg'] = $img;
}
This should work out!
on top of each page call:
session_start();
To safe variable in session for example:
$_SESSION['imageid'] = $ID
To get the variable back:
$imageid = $_SESSION['imageid']

Cannot display returned json data in CodeIgniter using jQuery autocomplete

I'm trying to move some AJAX code from a standalone file into a function in my controller and can't seem to get it to display the JSON data in the autocomplete function in the view. I've verified that the function does return JSON encoded data by visiting the function URL directly.
Here's my JavaScript from the head of my view:
<script type="text/javascript">
$(document).ready(function(){
var ac_config = {
source: <?php echo base_url() . 'admin/lookup_tmdb_movie_titles'; ?>
select: function(event, ui){
$("#title").val(ui.item.title);
$("#year").val(ui.item.year);
$("#imdb_link").val(ui.item.imdb_link);
},
minLength:2,
position: {
my: "left top",
at: "left bottom",
collision: "none",
of: "#title.ui-autocomplete-input.ui-autocomplete-loading"
}
};
$("#title").autocomplete(ac_config);
});
</script>
Here's the function in the admin controller (I'm just using hard-coded test data until I get this working correctly):
function lookup_tmdb_movie_titles()
{
$term = 'test';
// TEST DATA
$title['title'] = 'test';
$title['label'] = 'test (2012)';
$title['value'] = 'test';
$title['year'] = '2012';
$title['imdb_link'] = 'testlink';
$matches[] = $title;
// convert into JSON format and output
$matches = array_slice($matches, 0, 5);
$this->output->set_output( json_encode($matches) );
}
I've also tried outputting the JSON the following two ways, all of which work if I go to the function directly via the URL, but none of which work in the view itself.
print json_encode($matches);
and
$data['json'] = json_encode($matches);
$this->load->view('admin/json_view', $data);
I've looked at a lot of posts on StackOverflow and via Google (hence the different output methods above) but nothing has seemed to solve the issue yet.
first you should try this:
source: "<?php echo site_url(admin/lookup_tmdb_movie_titles); ?>"
then this could be:
function lookup_tmdb_movie_titles()
{
$term = 'test';
// TEST DATA
$title['title'] = 'test';
$title['label'] = 'test (2012)';
$title['value'] = 'test';
$title['year'] = '2012';
$title['imdb_link'] = 'testlink';
$matches[] = $title;
// convert into JSON format and output
$matches = array_slice($matches, 0, 5);
var_dump($matches); //comment this if everythings ok
// echo json_encode($matches); ->uncomment this if everythings ok
}
then open firebug launch the ajax request and check the response in console.

Incorrect characters in output

I'm trying to learn web scraping with Xpath. The code below works, however the output contains of incorrect characters and I can't manage to get this right.
Example:
Output: Emåmejeriet
How it should be: Emåmejeriet
PHP Code:
<?php
// Tried with these parameters but they doesn't make any difference
$html = new DOMDocument('1.0', 'UTF-8');
$html->loadHtmlFile('http://thesite.com/thedoc.html);
$xpath = new DOMXPath($html);
$nodelist = $xpath->query("//table");
foreach ($nodelist as $n) {
echo $n->nodeValue."\n";
}
?>
What can I do to fix this?
You should try encode() & decode() php functions if using ISO8859-15 or iconv() if not.
Example :
<?php
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", "ISO-8859-1");
?>

Resources