0

I have made a web page with a dropdown menu. The idea is when we click any option of the dropdown menu it open produccion.php with a POST parameter 'edicion'. This parameter I use to do a query in a Database.

At moment the problem is that it's open the produccion.php but I think that doesn't POST the value of the variable

Jquery code:

    $('#drEdicion a').click(function(event){
        alert($(this).text());

        var ed = $(this).text();

        $.ajax({  
            url:"produccio.php",  
            type:"POST",  
            data:{edicion: ed},  
            dataType:"text",  
            success:function(data){  
                window.location.href = 'produccio.php'
            }  
        });     

    });

Is it possible that the problem is when it's send via ajax?

Thanks in advance.

5
  • 1
    First you are sending ajax to produccio.php and then redirect page again to produccio.php... Where is the logic?! Beside, when you second time reditect page to produccio.php then of course there is not POST data because at second time you do not send any POST data :/ Commented Oct 25, 2016 at 8:49
  • @nospor I need only send a time, How do it? Commented Oct 25, 2016 at 8:51
  • 1
    Remove this window.location.href = 'produccio.php' Commented Oct 25, 2016 at 8:52
  • Are you trying to simulate a form POST, but with a hyperlink instead of a submit button? Commented Oct 25, 2016 at 9:03
  • Yes, I want to open produccion.php after selection option in dropdown button. With this option I do a query. Commented Oct 25, 2016 at 9:39

2 Answers 2

1

Your code is currently POSTing the content of the hyperlink over to produccio.php via AJAX. When that call completes it is then redirecting the user to the same page but without the POSTed value. Based on your comment:

I want to open produccion.php after selection option in dropdown button. With this option I do a query

it appears you actually want to open produccio.php with the POSTed value in the browser.

By far the easiest solution to the problem would be to alter the PHP code to accept the value of edicion from $_GET["edicion"] instead, and output your hyperlink as:

<a href="produccio.php?edicion=...">...</a>

This will also provide a better experience for users, especially if they have issues with JavaScript (or, for example, happen to be a search engine). However, if you're set on only using $_POST, the following code may - I've not tested this myself - allow this:

$('#drEdicion a').click(function(event){
    event.preventDefault();

    var ed = $(this).text();
    $('<form action="produccio.php" method="POST">' + 
          '<input type="hidden" name="edicion" value="' + ed + '" />' +
          '</form>').appendTo('body').submit();
});

(with credit to the top answer on Dynamically create and submit form)

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

Comments

0

Follow bellow code :

$('#drEdicion a').click(function(event){
        //alert($(this).text());
        event.preventDefault();
        var ed = $(this).text();

        $.ajax({  
            url:"produccio.php",  
            type:"POST",  
            data:{edicion: ed},  
            dataType:"html",  
            success:function(data){  
                //#dropdown-menu id show all produccio.php data
                $("#dropdown-menu").html(data);
            }  
        });     

});

produccio.php

<?php
   $edicion = $_POST['edicion'];
   //now you can able to access edicion post param
?>

HTML like this :

<div id="dropdown-menu"></div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.