Reputation: 1
Whats wrong here? Im trying to use a JS function that transforms a string:
?>
//JS
<script type="text/javascript">
var strIn = <?php echo json_encode($HTTP_RAW_POST_DATA); ?>;
var strKey = key
Decrypt3 = function (strIn, strKey) {
var strOut = new String();
var lenIn = strIn.length;
var lenKey = strKey.length;
var i = 0;
var numIn;
var numKey;
while (i < lenIn) {
numIn = parseInt(strIn.substr(i, 2), 32);
numKey = strKey.charCodeAt(i / 2 % lenKey);
strOut += String.fromCharCode(numIn - numKey);
i += 2;
}
return strOut;
$.post('shop_list.php', {variable: strOut});
};
</script>
//JS
<?php
$strOut = $_POST['strOut'];
And it gives me:
<b>Notice</b>: Undefined index: strOut in <b>shop-list.php</b> on line <b>40</b><br />
Isn't strOut
correctly defind here? What i can do to fix this problem?
Upvotes: 0
Views: 66
Reputation: 13263
There are a lot of things wrong with your code. In no particular order:
The Decrypt3
function is never called.
$.post
is never called because it comes after the return statement. You probably intended the return statement to come after the POST request, like so:
$.post('shop_list.php', {variable: strOut});
return strOut;
The json_encode()
variable will cause a syntax error. You have to throw it in quotes:
var strIn = <?php echo "'", str_replace("'", "\\'", json_encode($HTTP_RAW_POST_DATA)), "'"; ?>;
The strIn
and strKey
variables, defined outside the function, are never going to be used for anything. Inside the function they will be overridden by the function's arguments. You probably intended for them to be passed to the function, yes?
The key
variable is undefined. Problematic.
When grabbing the variable in PHP you are using $_POST['strOut']
, yet in your Javascript code you are sending it as $_POST['variable']
. You should fix that.
The numIn
variable is set to a JSON string, yet inside the function you are coercing it to an integer. That is not going to work. JSON strings, in PHP, look like {...}
or [...]
. That cannot be coerced into an integer.
I tried to refactor the code and this is what I came up with. It will probably not work (because of problem #7, for instance), but it was the best I could do to clean up the code.
<script type="text/javascript">
function Decrypt3(input, key) {
var out = "";
for (var i = 0; i < input.length; i += 2) {
var numIn = parseInt(input.substr(i, 2), 32);
var numKey = key.charCodeAt(i / 2 % key.length);
out += String.fromCharCode(numIn - numKey);
}
$.post('shop_list.php', {strOut: out});
return out;
};
var strIn = <?php echo str_replace("'", "\\'", json_encode($HTTP_RAW_POST_DATA)); ?>;
var strKey = "KEY OF SOME KIND...";
Decrypt3(strIn, strKey);
</script>
<?php
$strOut = $_POST['strOut'];
?>
Upvotes: 0
Reputation: 318182
You're sending an object and the key is variable
$.post('shop_list.php', {variable: strOut});
which means it's accessible with
$strOut = $_POST['variable'];
strOut
is just the javascript variable referencing the value
And you have to remove the return statement, otherwise the $.post function never runs
Upvotes: 2