0

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?

3
  • Are the two lines at the bottom part of shop_list.php? Are the other lines part of a different page? Commented Nov 18, 2014 at 4:53
  • They are the on the same php file. Which is shop-list.php Commented Nov 18, 2014 at 4:53
  • 1
    In your code What's key? Commented Nov 18, 2014 at 4:57

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

The POST request also happens after the return statement.
This gives me <b>Notice</b>: Undefined index: variable in <b>shop-list.php</b> on line <b>31</b><br />
Then try debugging, add print_r($_POST); in the PHP file, and a success handler to see what is returned
0

There are a lot of things wrong with your code. In no particular order:

  1. The Decrypt3 function is never called.

  2. $.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;
    
  3. 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)), "'"; ?>;
    
  4. 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?

  5. The key variable is undefined. Problematic.

  6. 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.

  7. 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'];
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.