0

scenario is:

I have HTML a-tag in a page(call it 'index.php') which is a link to another page (lets call it page 'action.php') ! obviously

'action.php' page needs to receive a POST parameter, otherwise it return error!

i have the needed parameter in 'index.php' page, but i dont want to use form-tag to send it. Whenever user clicked on the a-tag, the parameter should be sent to the 'action.php' page.

Indeed, AJAX has nothing to do here, while i want to change the page, with sending the POST parameter whenever user click on my < a > !

example: index.php

<a class='showProject' href='action.php' data-pid='<?php echo project_ID ?>'> PROJECT </a>

action.php

 echo $_POST['project_id'];  

i prefer that, if there is any way to do this, the jQuery Code starts with the following event:

$('.showProject').on('click',this,function(e){

     /* YOUR SOLUTION */
});

2 Answers2

1

You can use Ajax for this. I don't know where you have the parameter you want to pass to action.php, but in my example it's stored in a hidden value.

$('.showProject').click(function() {
  var productId = $("#hiddenField").Value();
    $.ajax({
      type: 'POST',
      url: 'action.php',
      data: {productId: productId},
      success: function(data) { 
              //Do whatever you need to do with the return data.
          },
      error: function(xhr, ajaxOptions, thrownerror) { }
     });
});
MichaelCleverly
  • 2,473
  • 1
  • 24
  • 33
0
<span class='showProject' data-action-url='action.php' data-pid='<?php echo project_ID ?>'> PROJECT </span>

If you want to use Ajax don't use <a> Tag. instead use a span or button.

If you use <a> tag, set href='#' or href="javascript:void(0)"

<a class='showProject' href='#' data-action-url='action.php' data-pid='<?php echo project_ID ?>'> PROJECT </a>

OR

<a class='showProject' href='javascript:void(0)' data-action-url='action.php' data-pid='<?php echo project_ID ?>'> PROJECT </a>

Then use Ajax

$('.showProject').on('click',this,function(e){

    $.ajax({
           method: 'POST',
           data: { project_id: $(this).data('pid')},
           url: $(this).data('action-url'),
           success: function(data){

           // HERE Assign Returned page to DIV

           } 
     });
});

EDIT: I have never worked in PHP. But what you are trying to do doesn't seem right.

Create a new file.

controller.php

Instead of going direct to action.php, POST to the controller.php.

$('.showProject').on('click',this,function(e){

    $.ajax({
           method: 'POST',
           data: { project_id: $(this).data('pid')},
           url: 'controller.php',
           success: function(data){

           // Redirect to Page:
             window.location.href = URL RETURN FROM PHP;

           } 
     });
});

Store the id in the session.

// this syntax maybe wrong

$_SESSION["prod_id"] = $_POST['project_id'];

return URL // I don't know how to do that in PHP

In action.php, get the ID

echo $_SESSION["prod_id"];
Dawood Awan
  • 7,051
  • 10
  • 56
  • 119
  • thanks for your solution, but i want to change the page to 'action.php' with the POST parameter included , whenever user clicked on < a >, not just assign the result contents to a div in the same page (index.php) – Amin Akhyani Apr 29 '15 at 14:07
  • something like this? http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – Dawood Awan Apr 29 '15 at 14:08
  • why are you using POST to load a page? – Dawood Awan Apr 29 '15 at 14:25