1

When $posting with a button click the data is not sent the first time. e.g no results are shown. if however I use the $post again the data is sent but it is the previous data. So it is always one step behind with data that is being received and blank when first trying.

So far this is what I have. P.S this is just a offline game for me to play so not a serious thing. To pass the data down:

if (this.id === me.id && this.checked) {
    var str = $(this).attr('id');
    var ret = str.split(" ");
    var str1 = ret[0];
    var str2 = ret[1]
    
    alert(str1);
    alert(str2);
     
    if (str1) {      
        var variableToSend = str1;
        $.post('breeding2.php', {variable: variableToSend});
    }
}

breeding2.php

if (!empty($_POST['variable'])) {
    $_SESSION['testfemale'] = $_POST['variable'];


    $testfem2 = $_SESSION['testfemale']; //testing
  
    $insertrecord = "INSERT INTO animal (species, gender, status,owner,past,age,birth,currentagemonths,currentageyear,purchase,type,colour,sale,minprice,maxprice,rate,cond,gestation,breedrest,timesbred,update_time,maid,mpar,feed,selling, roomid, back)
    VALUES ('$animalspecies', '$randomgender', '$status', '$playersession','$playersession', 'unborn', NULL, '0', '0', NULL, '$animaltype','$pattern','$price','$price','0','0.0',NULL, NULL,NULL, NULL,NOW(),'$testfem2','$malevalue','unfed','unsold','0','$envir')";
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    Where do you display it? From the code alone it's not visible, and I'd say that $.post() and the php works, it is perhaps more with retrieving the data again and displaying it. – hakre Aug 05 '23 at 14:56
  • and in javascript you will likely have more fun with console.log() then alert(). see https://developer.mozilla.org/en-US/docs/Web/API/console#examples and then for your browser how to show the console. – hakre Aug 05 '23 at 14:58
  • it has a insert into database in the if empty forgot to add it on here. First time it just adds a blank into my database. next time it adds the correct variables. But it is a delay as it shows the previous variable so always one behind – Jennifer Cornhill Aug 05 '23 at 15:02
  • Hmm, not sure I can fully follow, I did already understood that part from your question, perhaps it's that I can't see where that database operation is? In the PHP code in question there is only the assignment to the session variable. /E; And if it helps, similar to console.log() in the browser, for PHP there is error_log(). When you use the PHP development server you see it then immediately in the console where you started it. – hakre Aug 05 '23 at 15:04
  • 1
    Your PHP is wide open to SQL injection - you should always use a `prepared statement` with `bound parameters` when using input from the user in your sql queries – Professor Abronsius Aug 05 '23 at 15:08
  • when a checkbox id is checked it is posted and inserted into the database. for example if it is one then it correctly inserts into the database as one. and if checkbox id is 2 it shows correctly as 2 works but when first attempting it and clicking checkbox 1, it displays blank, but when i click the checkbox id 2 it will alert for 2 but it will display the previous 1 then if i check checkbox 3 it will alert for 3 but will insert previous variable 2 not sure if that helps ? – Jennifer Cornhill Aug 05 '23 at 15:08
  • 1
    Please add the relevant HTML to support the above code fragments so we might understand better why your problem is happening – Professor Abronsius Aug 05 '23 at 15:09
  • yes i know but this is just a private game for me and my friends – Jennifer Cornhill Aug 05 '23 at 15:09
  • 1
    If I were your friend I'd try to exploit it for fun... – Professor Abronsius Aug 05 '23 at 15:10
  • no we are just practicing and having fun will secure in given time – Jennifer Cornhill Aug 05 '23 at 15:11
  • 1
    @JenniferCornhill: Apart from the SQL injection, I'll fetch you a link for more info, but first of all, this is only the query but not how you're executing it. e.g. if there is no error checking, we can't say what happens (and that is also in part b/c of the SQL injection, those queries can easily _break_ and then the program does not do any longer what you want it to do). There are two parts in security, one is the shady/interesting side, but there is also the side that you take care that the program works for sure/securely (operational security, stability). – hakre Aug 05 '23 at 15:12
  • Ah right I see as it executes fine after first post just variable is one behind – Jennifer Cornhill Aug 05 '23 at 15:14
  • But it's not clear what that means as you hide the code. Normally when that happens it is that you encoded it that way. You're in control/command. – hakre Aug 05 '23 at 15:15
  • sorry im quite to but what do you mean hide the code? – Jennifer Cornhill Aug 05 '23 at 15:16
  • found the issue it seems the first time $_post is used it always returns a null – Jennifer Cornhill Aug 05 '23 at 17:12
  • It's all a bit unclear. Are you loading breeding2.php direct in your browser _and_ then calling it via ajax? That could easily explain the behaviour you describe. – ADyson Aug 05 '23 at 22:16
  • Yeah I am posting it to itself. It posts fine but only after posting again. The first time always returns a null e.g. I have a echo no if returning no and that's what it displays but only the first time – Jennifer Cornhill Aug 06 '23 at 08:55
  • So if it is posting to itself, then, as I suspected, the "first time" is actually the time when you originally load the breeding2.php into the browser. That, naturally, is _before_ you have posted anything, and so of course you see null as the output, because nothing was inserted yet. (I'm guessing a bit of course because you haven't actually provided a [mre] showing how the output from php is being displayed. You can [edit] your post to clarify it) – ADyson Aug 06 '23 at 19:04
  • It sounds like your problem is related to the simple face that each HTTP request (page load) involves a request from the server (your PHP part) , which is sending a response to your browser (HTML/JavaScript part). When the response is rendered, the request is complete (finished). Hence you seem to be one step behind, because it's not clear is you keep session state in your PHP script. It's recommended that you edit your question and provide short, simple but complete working examples of both your server-side (PHP) and client-side (JavaScript) scripts - the should lead to a clear solution – Werner Aug 06 '23 at 23:04

0 Answers0