Passing php param to Js redirect

I have declared the following variable

var Id = document.getElementById("actie_id").value;

Than in my AJAX success function I have the following redirect

window.location.href = "/admin/nieuws/nieuws_details/" + Id;

But it isn’t redirecting?

Thank you in advance

There has to be an octothorpe (hash sign) between the URL and the fragment identifier, that being the id.

For example: /admin/nieuws/nieuws_details/#actie_id

Hi Paul, so

Should be

var Id = document.getElementById("#actie_id").value;

You mean?

No - it should be the href assignment that you need to adjust.

Hang on - what are you wanting to achieve?

Are you wanting to scroll the destination page down to a certain identifier? If not, are you wanting instead to load a separately named page?

Letting us view a test page that demonstrates the problem, will help us to provide a rapid and timely answer.

Hi Paul. I haven’t a test page a yet. What I need is to redirect to a detail page after an update was been done. The redirect should hold the id (in order to go to the right detail page). The id is as hidden field in the form So the redirect should for example be:

/admin/nieuws/nieuws_details/1

Hope this is understandable?

Does using example.com/admin/nieuws/nieuws_details/1 just by itself, result in successfully loading the page?

It is redirecting now but I get an Undifined error now for the param

Do you know what the resulting URL needs to be, for things to work?

It is always /admin/nieuws/nieuws_details/ followed by the id from the record that just has been updated

This is the complete AJAX success function:

        success: function(data){
            window.location.href = "/admin/nieuws/nieuws_details/" + data.actie_id;                         
        }

and this is actie_id in the form:

<input name="actie_id" type="hidden" value="<?php echo $acties['actie_id']; ?>">

Which of the following though is the sort of link that you have to use?

  • /admin/nieuws/nieuws_details/1
  • /admin/nieuws/nieuws_details/#1
  • /admin/nieuws/nieuws_details/?id=1

Hi Paul. I was out for a while sorry.

I need to use the first one

Then what you had there in the original post should do the job well.

If it’s not working for you, it means that there is something else other than the code that you showed us that’s causing the problem. If that’s the case - nothing beats having someone else look at the page to find out what’s causing the problem.

Hi Paul I changed it wack to how it was, but than the redirect isn’t working. Is it possible that that is because I use:

data: $(this).serialize(), in the function as well?

There is something funny going on in this function, or maybe I am doing something completely wrong?

This is the complete function:

$("#nieuws_update_form").on("submit", function(e) {
    e.preventDefault();    
    var actie_id   = $("#actie_id").val(),
        titel      = $("#titel").val(),
        actie      = $("#actie").val(),
        eind_datum = $("#eind_datum").val();
    
    var dataString = 'titel='+ titel + '&actie='+ actie + '&eind_datum='+ eind_datum + '&actie_id='+ actie_id;
                 
    $.ajax({
        url : "/admin/nieuws/update_nieuws",
        type: "post",
        data: dataString,
        cache: false,
        success: function(data){
            window.alert("Content" + titel);
            window.location.href = "/admin/nieuws/nieuws_details/" + actie_id;                         
        },
        error: function(data){
            window.alert("Aanbieding/nieuws item is niet gewijzigd. Klik op de OK button en probeer het nogmaals.");
            setTimeout(function() {
                location.reload();
            }
            , 2000) 
        }
    });
});

When I use The redirect wit the actie_id like this:

window.location.href = "/admin/nieuws/nieuws_details/" + actie_id;    

I am redirected but actie_id is undefined. As soon as I add one of the other form values, for example

window.location.href = "/admin/nieuws/nieuws_details/" + title;    

the variable is defined but obviously I dont get the right detail values since in the details query the actie_id is the key to get the right values

I still don’t see what I am doing wrong?

So, you need to look at why it’s undefined.

Here is where you’re getting the value.

var actie_id   = $("#actie_id").val(),

Take a look at the element that you have with an id of “actie_id”

In fact, I see it further up, here it is.

<input name="actie_id" type="hidden" value="<?php echo $acties['actie_id']; ?>">

Make that input not hidden, so that you can see it on the page.
You will want to see a value there. If you see no value there, you know where to investigate further.

1 Like

If your Redirect to URL needs a post request then you can’t use ‘window.location.href’, You will have to have a hidden field in the form and then submit it with javascript to the page you are redirrecting too

<input type="hidden" name='id' value='<?php echo $yourid; ?>'>

otherewise its a GET request and you can just put ‘?id=YOURID’ at the end of the url and then use window.location.href

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.