Post to php variables in javascript - javascript

In php, I want to pass a php variable using the jquery.post method.
The first figure shows the code that post the variable
first
The second figure shows to read the posted variable.second
php variable contains some statements, but output will print the variable names like "$rxss", "$sxss".
The post method seems to be writing the data part incorrectly, so please let me know how to write it.
add
i already tested
echo '<script type="text/javascript">
$(document).ready(function() {
$.post("scanner/getStatus.php", {testId:' . "$testId" . ',chkCnt:' . "$chkCount" . ',rxss:' . "$rxss" . ',sxss:' . "$sxss" . ',sqli:' . "$sqli" . ',basqli:' . "$basqli" . ',autoc:' . "$autoc" . ',idor:' . "$idor" . ',dirlist:' . "$dirlist" . ',bannerdis:' . "$bannerdis" . ',sslcert:' . "$sslcert" . ',unredir:' . "$unredir" . ',clamav:' . "$clamav" . '}, function(data){$("#status").html(data)});
var refreshId = setInterval(function() {
$.post("scanner/getStatus.php", {testId:' . "$testId" . ',chkCnt:' . "$chkCount" . ',rxss:' . "$rxss" . ',sxss:' . "$sxss" . ',sqli:' . "$sqli" . ',basqli:' . "$basqli" . ',autoc:' . "$autoc" . ',idor:' . "$idor" . ',dirlist:' . "$dirlist" . ',bannerdis:' . "$bannerdis" . ',sslcert:' . "$sslcert" . ',unredir:' . "$unredir" . ',clamav:' . "$clamav" . '}, function(data){$("#status").html(data)});
}, 500); ......
-
echo '<script type="text/javascript">
$(document).ready(function() {
$.post("scanner/getStatus.php", {testId:' . "$testId" . ',chkCnt:' . "$chkCount" . ',rxss:"" + $rxss + "",sxss:"" + $sxss + "",sqli:""+ $sqli + "",basqli:"" + $basqli + "",autoc:"" + $autoc + "",idor:"" + $idor + "",dirlist:"" + $dirlist + "",bannerdis:"" + $bannerdis + "",sslcert:"" + $sslcert + "",unredir:"" + $unredir + "",clamav:"" + $clamav + ""}, function(data){$("#status").html(data)});
var refreshId = setInterval(function() {
$.post("scanner/getStatus.php", {testId:' . "$testId" . ',chkCnt:' . "$chkCount" . ',rxss:"" + $rxss + "",sxss:"" + $sxss + "",sqli:""+ $sqli + "",basqli:"" + $basqli + "",autoc:"" + $autoc + "",idor:"" + $idor + "",dirlist:"" + $dirlist + "",bannerdis:"" + $bannerdis + "",sslcert:"" + $sslcert + "",unredir:"" + $unredir + "",clamav:"" + $clamav + ""}, function(data){$("#status").html(data)});
}, 500); ......
but when these test not showing getStatus.php and apache log haven't error

You can you this $("$yourvariable")
Also take a look at this How to call PHP function from string stored in a Variable

Related

How do I add an onClick event to a link generated on PHP?

I am trying to add an onclick event on a link in a PHP file.
$body2 .= '<td class="sampling-list-td download-link">' . '<a ' . 'class="sampling-list-download" ' . 'href="#" ' . 'data-id="' . $assetId . '" ' . 'data-type="' . $assetType . '" ' . 'data-event="LogFileElaboration" ' . 'data-file="' . $fileName . '" ' . 'data-enterprise="' . $provisionedBuId . '"' . '>'
. '<i class="glyphicon glyphicon-download-alt" style="color: rgb(109, 110, 113); zoom: 1.3;"></i>'
. '</a>' . '</td>';
This is what I'm trying to do:
$body2 .= '<td class="sampling-list-download" ' . 'onClick="ga('send', 'event', { eventCategory: 'download', eventAction: 'click', eventLabel: 'Files Download'});" ' . 'href="#" ' . 'data-id="' . $assetId . '" ' . 'data-type="' . $assetType . '" ' . 'data-event="LogFileElaboration" ' . 'data-file="' . $fileName . '" ' . 'data-enterprise="' . $provisionedBuId . '"' . '>'
. '<i class="glyphicon glyphicon-download-alt" style="color: rgb(109, 110, 113); zoom: 1.3;"></i>'
. '</a>' . '</td>';
But I am getting the following errors:
ParseError: syntax error, unexpected 'send' (T_STRING)
I don't know what i'm doing wrong? I'm trying to add the event tracking code for google analytics T_T
the quote in 'send' ends the string you started right before onClick. When the string is terminated it will interpret as PHP again and send is not a valid keyword here.
To prevent this you can escape the ' like this: 'onClick="ga(\'send\', \'event\', { e...
Its a Quote problem .change like this add\ before ' in onclick.Otherwise each time ' .is break the string and after the text are execute like php statement or variable .If the php send is invalid one so you got error
$body2 .= '<td class="sampling-list-download" ' . 'onClick="ga(\'send\', \'event\', { eventCategory: \'download\', eventAction: \'click\', eventLabel: \'Files Download\'});" ' . 'href="#" ' . 'data-id="' . $assetId . '" ' . 'data-type="' . $assetType . '" ' . 'data-event="LogFileElaboration" ' . 'data-file="' . $fileName . '" ' . 'data-enterprise="' . $provisionedBuId . '"' . '>'
. '<i class="glyphicon glyphicon-download-alt" style="color: rgb(109, 110, 113); zoom: 1.3;"></i>'
. '</a>' . '</td>';

Loading Images from a Directory Rather Than Individually In Javascript Var

edit --
I was able to figure this out however I only want to display .png files that are found in my directory. Is there a way to only show png files by adding something to this code?
<?php
$path = 'testimages';
$handle = #opendir('./' . $path . '/');
while ($file = #readdir($handle)){
if("." === $file || ".." === $file) continue;
echo ', {' . "\n" . ' id: ' . preg_replace("#\.[^\.]+$#", "", $file) . ',' . "\n" . 'name: "Product ' . preg_replace("#\.[^\.]+$#", "", $file) . '",' . "\n" . 'imageUrl: "' . $file . '",' . "\n" . 'thumbnailUrl: "' . $file . '",' . "\n" . 'openUrl: "' . preg_replace("#\.[^\.]+$#", "", $file) . '.html"' . "\n" . '}' . "\n";
}
#closedir($handle);
unset($handle,$file,$path);
?>

Weird issue with PHP and JS scripts, fails the first time until I refresh the page

I have a PHP page which displays a picture :
echo "<p> <img id=\"myPic\" src=\"myPic.png\" > </p>";
Later in the code, i'm placing small icons around that picture, using JS :
echo "<script>
var picTop = document.getElementById(\"myPic\").offsetTop ;
var picLeft= document.getElementById(\"myPic\").offsetLeft ;
document.getElementById(\"" . $iconId. "\").style.top = picTop + " . $iconPosition[$i][0] . " + \"px\";
document.getElementById(\"" . $iconId. "\").style.left = picLeft + " . $iconPosition[$i][1] . " + \"px\";
</script>";
I uploaded the page on a server, and other users told me that the icons were overlapping the main picture. However this only happens once, and it is fixed when they reload the page. I've witnessed that too, and i could reproduce the issue by using another browser, again solved by refreshing.
I have no idea what's wrong. Perhaps the main picture takes too long to load, and the JS script has already been performed, so the icons are badly positioned - but it is later fixed because the picture is in the temporary files, or something.
I tried to delay the JS script with a setTimeout, but still didn't fix the problem
echo "<script>
var picTop = document.getElementById(\"myPic\").offsetTop ;
var picLeft= document.getElementById(\"myPic\").offsetLeft ;
setTimeout ( function() {
document.getElementById(\"" . $iconId. "\").style.top = picTop + " . $iconPosition[$i][0] . " + \"px\";
document.getElementById(\"" . $iconId. "\").style.left = picLeft + " . $iconPosition[$i][1] . " + \"px\";
}, 500) ;
</script>";
or maybe i could try with more milliseconds... i can't make a proper diagnosis due to the difficulty to reproduce the issue
Thanks for your help !
echo "<script>
document.addEventListener( 'DOMContentLoaded', function () {
var picTop = document.getElementById(\"myPic\").offsetTop ;
var picLeft= document.getElementById(\"myPic\").offsetLeft ;
document.getElementById(\"" . $iconId. "\").style.top = picTop + " . $iconPosition[$i][0] . " + \"px\";
document.getElementById(\"" . $iconId. "\").style.left = picLeft + " . $iconPosition[$i][1] . " + \"px\";
}, false );
</script>";

Output from database use toggle to edit data

I have a database with some record which i output on my page using php. This all works fine but i want to be able to edit the outputted information using the toggle functionality. So basicly i output text when a button edit is clicked i want to toggle that specific table and show the information once more but then in an input form.
foreach( $result as $row2 )
$div = $row2['id'] ;
echo "<div id='d$div; '><table ><tr><td>" . 'Categorie: ' . '</td><td> ' . $row2['name'] . '</td></tr>'
. '<tr><td>' . 'Omschrijving: ' . '</td><td>' . $row2['comments'] . '</td></tr> . '<tr><td>' . '<button id="e1">' . 'Edit' . '</button>' . '</td></tr>' . ' </table><hr>'; } ?></p>
If I would then would then echo everything out again using <form> input fields and use as div id f$div every record would get its unique id.
foreach( $result as $row2 )
$di = $row2['id'] ;
echo "<div id='f$di; '><table ><tr><td>" . 'Categorie: ' . '</td><td> ' . $row2['name'] . '</td></tr>'
. '<tr><td>' . 'Omschrijving: ' . '</td><td>' . $row2['comments'] . '</td></tr> . '<tr><td>' . '<button id="e2">' . 'Safe' . '</button>' . '</td></tr>' . ' </table><hr>'; } ?></p>
Then I would toggle it but I don't have the ids as they still have to be generated. How would I go about this...same would go for the click function but I can generate an id for these in the same way.
<script>
$('#e1,#e2').click(function () {
$('#d?,#f?').toggle();
})
</script>
You can do this with javascript, too. This is one example. You can even use AJAX to update, otherwise you can update the data in database and come back to the same page.
<?php
foreach($result as $row2) {
$div = $row2['id'];
echo "<div id='d$div'><table ><tr><td>" . "Categorie: " . "</td><td> " . $row2["name"] . "</td></tr>" . "<tr><td>" . "Omschrijving: " . "</td><td>" . $row2["comments"] . "</td></tr>" . "<tr><td>" . "<button id=e$div onclick='makeForm(\"$div\",\"d$div\",\"$row2[name]\",\"$row2[comments]\")';>" . "Edit" . "</button>" . "</td></tr>" . "</table><hr></div>\n";
}
?>
<script>
function makeForm(id,mydiv,category,comment){
var div = document.getElementById(mydiv);
var divHTML = "<form action='update.php' method='POST'><table> <tr><td>Categorie: </td> <td><input type='text' name='cat_" +id+ "' value='"+category + "' /></td></tr> <tr> <td>Omschrijving:</td> <td><input type='text' name='comment_" +id+ "' value='"+comment+"' /></td></tr> <tr><td> <input type='submit' value='Safe'></td><td><button onclick='removeForm(\""+id+"\",\""+mydiv+"\",\""+category +"\",\" "+comment+"\" ); return false;'>Cancel</button></td></tr></table> </form><hr>";
div.innerHTML = divHTML;
}
function removeForm(id,mydiv,category,comment){
var div = document.getElementById(mydiv);
var divHTML = "<table> <tr><td>Categorie: </td> <td>"+category + "</td></tr> <tr> <td>Omschrijving:</td> <td>"+comment+"</td></tr> <tr><td><button onclick='makeForm(\""+id+"\",\""+mydiv+"\",\""+category +"\",\" "+comment+"\" ); '>Edit</button></td></tr></table><hr>";
div.innerHTML = divHTML;
}
</script>
Hope this helps...

New line in alert window with jQuery jqXHR responseText

I'm currently requesting some text via jQuery on a php page. The problem is that the new line symbols \n are displayed in the alert window, and do not get interpreted as new line. I do not escape the string at any time.
In the error: function(..) block of my jQuery request, I get the string returned by php via jqXHR.responseText, here is it's exact value :
Some fields are empty!\n$name= \n$schedule= 480;1140;480;1140;480;1140;480;1140;480;1140;480;1140;480;1140\n$free_connection= false\n$free_coffee= false\n$rating= -1\n$lat= 44.715513732021336\n$lng= 1.9775390625\n
When I do this, new lines DON'T WORK :
alert(jqXHR.responseText);
So to debug, I decided to paste the php response string (which is the previous string) directly in the alert() function, and in this case the new lines WORK :
alert("Some fields are empty!\n$name= \n$schedule= 480;1140;480;1140;480;1140;480;1140;480;1140;480;1140;480;1140\n$free_connection= false\n$free_coffee= false\n$rating= -1\n$lat= 44.715513732021336\n$lng= 1.9775390625\n)");
The string is exactly the same, but yet new lines don't work when I use jqXHR to get it.
Anybody has any clue ?
EDIT: php code that generates the string :
$errorMessage = 'Some fields are empty!' . '\n'
. '$name= ' . $name . '\n'
. '$schedule= ' . $schedule . '\n'
. '$free_connection= ' . $free_connection . '\n'
. '$free_coffee= ' . $free_coffee . '\n'
. '$rating= ' . $rating . '\n'
. '$lat= ' . $lat . '\n'
. '$lng= ' . $lng . '\n';
echo $errorMessage;
You're using the wrong type of quotes. In PHP, escape sequences aren't processed in single quotes, only in double quotes. So it should be:
$errorMessage = 'Some fields are empty!' . "\n"
. '$name= ' . $name . "\n"
. '$schedule= ' . $schedule . "\n"
. '$free_connection= ' . $free_connection . "\n"
. '$free_coffee= ' . $free_coffee . "\n"
. '$rating= ' . $rating . "\n"
. '$lat= ' . $lat . "\n"
. '$lng= ' . $lng . "\n";
echo $errorMessage;
I do not know if I understood well but You can try
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
or
$.getJSON( "ajax/test.json", function( data ) {
var items = [];
$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val + "</li>" );
});
$( "<ul/>", {
"class": "my-new-list",
html: items.join( "" )
}).appendTo( "body" );
});

Categories

Resources