0

I am back to development and need some help or tip.

I want to POST a variable using AJAX on another page but it is not working. My post variable is empty.

my code is (simplified version)

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script>
    function logWriter(x){
    var str;
   switch(x) {
   case "A": 
      str = "hello1";
       break;
   case "B": 
       str = "hello2";
       break;
   default: 
       //default
   }
   
    $.ajax({
      type: 'POST',
      url: 'm.php',
      data: { 'field1': str },
   });
};
</script>

<div>
    <a href="m.php" onclick = "logWriter('A');">Link 1</a>
    <a href="m.php" onclick = "logWriter('B');">Link 2</a>
</div>

the code for m.php

<?php
if(isset($_POST['field1']))
echo $_POST['field1'];
?>

I already searched for this problem over StackOverflow but no success :(

How to pass parameters in $ajax POST?

Can't print POST variable from AJAX using PHP

can anyone guide me in right direction. Thank you

nikki
  • 1
  • use console.log() and inside put the get from action $.ajax({ ... post .. and use post requests with 'm.php?'+Date.now() to archieve different calls and so avoid cache errors –  Apr 07 '22 at 10:15
  • follow up I tried to check with Chrome Developer tools --> network fetch/XHR my variable value is coming from Ajax on another page but it is not showing on the page – nikki Apr 07 '22 at 10:38
  • follow up i tried to take the varibale in session and it is working – nikki Apr 07 '22 at 10:47
  • so somehow the variable is reseating the value on page load – nikki Apr 07 '22 at 10:48
  • which i need to figure it out – nikki Apr 07 '22 at 10:48
  • "*my variable value is coming from Ajax on another page*" - does it mean you see the response from PHP in devtools? "*it is not showing on the page*" - it won't show up on the page until you add some JS which adds it *on* the page ...? – Don't Panic Apr 07 '22 at 10:49
  • Are you asking how to update the page with the AJAX response? – Don't Panic Apr 07 '22 at 10:54
  • console.log using is meant to run in browser press ctrl+shift+K when you open a new tab then run your script to see the results or refresh F5 key to see results again –  Apr 07 '22 at 10:54
  • @Don'tPanic I want to do following I have a main page JKL.php on the main page i have a link in A HREF tag which is leading to page ABC.php i want to take some value from main page (JKL.php) to a third page which is XYZ.php **when the user is clicking on the link which is in HREF tag** in short two events are happening 1. user is going to ABC.php page 2. Ajax is sending the value to XYZ.php i think my problem is these two event occurance which is reseting the value of my post variable but I am not sure – nikki Apr 07 '22 at 11:18
  • Where is the 3rd page in your code above? I suggest editing your question, and trying to create a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) which shows the problem. Currently it is not clear what you are trying to do, nor what the problem is. – Don't Panic Apr 07 '22 at 11:30

0 Answers0