-1

Is that possible to pass a value without using form & hidden field? I try the ajax but not working. Below is my code

../order/?step=0 code:

<script>
jQuery(document).ready(function($){ 
    var step0 = [];
    var item = {
        pid: 123,
        qty: 1
    };
    step0.push({0:item});
    data = JSON.stringify(step0);
    $("a.base-product").click(function() {  
        $.ajax({
            type: 'POST',
            url: '../order/?step=1',
            data: data,
        });
    });
});
</script>

<a class= "base-product" href="../order/?step=1">next</a>

After I click the next link, will need to ask the data from ajax to ../order/?step=1

../order/?step=1:

print_r($_POST);

But I get Array();

Or is that able to use the $_SESSION?

Ying Rui Wong
  • 113
  • 3
  • 17
  • Please specify ContentType header as application/json. refer this: https://forum.jquery.com/topic/ajax-with-contenttype-application-json – Ansari Maksud Aug 24 '20 at 04:06
  • @MakAnsari I want to post value from page A to page B, the situation not same as your link. – Ying Rui Wong Aug 24 '20 at 06:11
  • When you press 'next' link then you wanted to pass some hidden data from current page to order page. right?? – Ansari Maksud Aug 24 '20 at 06:53
  • @MakAnsari nope. My question is pass the value without having form or hidden input when I clicked "next" – Ying Rui Wong Aug 24 '20 at 07:03
  • The `?step=1` part is in the link, not part of the POST data. So use `$_GET` to retrieve – Michel Aug 24 '20 at 08:10
  • @Michel I want to post the {pid:123, qty:1} from ?step=0 to ?step=1. This data can't not show public.I am not try to get the step number in url param. – Ying Rui Wong Aug 24 '20 at 09:04
  • PHP does not populate $_POST, if you send JSON. In that case, you need to read it using this method, https://stackoverflow.com/questions/18866571/receive-json-post-with-php – CBroe Aug 24 '20 at 09:12
  • @CBroe thanks. I tried and it only return me string(0) "" or blank or Null, I event use the contentType in ajax. Is this not possible to POST via ajax? – Ying Rui Wong Aug 24 '20 at 09:28

1 Answers1

0

<form>
  <div>
    <label for="title">Post title:</label>
    <input type="text" id="title" name="title" value="My excellent blog post">
  </div>
  <div>
    <label for="content">Post content:</label>
    <textarea id="content" name="content" cols="60" rows="5">
This is the content of my excellent blog post. I hope you enjoy it!
    </textarea>
  </div>
  <div>
    <button type="submit">Update post</button>
  </div>
  <input type="hidden" id="postId" name="postId" value="34657">
</form>