Rajan
Rajan

Reputation: 2425

Can we pass GET variables in javascript url?

After form submission I want to redirect to another page where I need values from the Previous submitted form.

So in my redirect script I want to pass those values in GET

I am doing this :

<script>
var id = <?php echo $data['base_ini_id']."?&ext=".$data['ext']; ?>

window.location.replace("./edit_ini_custom/"+id);
</script>

OR Is there anything else with which I can do as required?

Upvotes: 0

Views: 78

Answers (4)

tanovellino
tanovellino

Reputation: 73

Get variables are (always) passed through the URL. So basically you should use:

var redirectUrl = "yourUrlWithVariables";
window.location.replcae(redirectUrl);

Now depending on the format of your target URL, the way you compose the target URL. Here are some common examples: Format 1: baseUrl.com/page?paramName1=paramValue1&paramName2=paramValue2

var baseId = '<?=$data['base_ini_id'];?>';
var extId = '<?=$data['ext'];?>';
var baseUrl = "baseUrl.com/edit_ini_custom?base_ini_id="+baseId+"&ext="+extId;

Format 2: baseUrl.com/page/paramValue

var extId = '<?=$data['ext'];?>';
var baseUrl = "baseUrl.com/edit_ini_custom/"+extId;

Format 3: baseUrl.com/page/paramName/paramValue

var extId = '<?=$data['ext'];?>';
var baseUrl = "baseUrl.com/edit_ini_custom/ext/"+extId;

Format 4: baseUrl.com/page/paramName1/paramValue1/paramName2/paramValue2

var baseId = '<?=$data['base_ini_id'];?>';
var extId = '<?=$data['ext'];?>';
var baseUrl = "baseUrl.com/edit_ini_custom/base_ini_id/"+baseId+"/ext/"+extId;

Note that this are only guidelines, and in any case after you parse and build the target URL, you must call window.location.replace(baseUrl). Other option is just use: window.location = baseUrl; but using this will add that redirection to the history allowing users to hit "back", which I'm not sure is something you'd like.

Hope this helps

Upvotes: 2

Rajan
Rajan

Reputation: 2425

Finally Got an Answer:

<script>
var id = <?php echo $data['base_ini_id']; ?>

window.location.replace("./edit_ini_custom/"+id+"/"+<?php echo $data['ext']; ?>);
</script>

I can pass the PHP Variable as PARAMETER And On the Function Where I need it i Just Have to Do :

public function edit_ini_custom($id,$ext)
{
   echo $ext;
   //code
}

Upvotes: 0

Imesha Sudasingha
Imesha Sudasingha

Reputation: 3570

You can do a PHP redirect by setting the header Location

header("Location: ".$data['base_ini_id']."?&ext=".$data['ext']);

This will be a convenient way i guess than the one you suggested.

Upvotes: 0

Sarath Chandra
Sarath Chandra

Reputation: 1878

The form that's already submitted (in the previous page) cannot be accessed, as such, from the next page.

So, in short, the answer to your question is NO.

You need to implement in one of the ways:-

  • Store the values in the user's session object (if you already have login implemented)
  • Use a Http Cookie or Browser's localStorage to store the value
  • Persist the value to the server and fetch in the subsequent page, as hidden variables.

Upvotes: 0

Related Questions