user1973756
user1973756

Reputation:

How to pass values of $_GET array to new URL?

I have a simple form, when the user entered a code and a mobile number, it should open a new url, here is my code:

<form method='get'>
ID:     <input type='text' name='code'><br>
Mobile: <input type='text' name='mobile'><br>

<?php

echo "<button type='button' onclick='location.href=\"http://workflow.abfarmarkazi.ir/phpsql/sms_info.php?id={$_GET['code']}&mob={$_GET['mobile']}\"'>Submit</button>";

?>

</form>

but the values of $_GET array will not updated, what should I do?

Upvotes: 0

Views: 301

Answers (4)

Mohit S
Mohit S

Reputation: 14064

<form method='get' action='http://workflow.abfarmarkazi.ir/phpsql/sms_info.php'>
    ID:     <input type='text' name='id'><br>
    Mobile: <input type='text' name='mob'><br>
    <button type='button' onclick='this.form.submit();'>Submit</button>";
    </form>

The $_GET superglobal is defined as part of the URL string:

http://workflow.abfarmarkazi.ir/phpsql/sms_info.php?code=123&mobile=25642356

In sms_info.php:

echo $_GET['code']; // 123
echo $_GET['Mobile']; // 25642356

So $_GET is not stored on the server, but is passed with each HTTP request, as is $_POST, but that is passed in the HTTP headers rather than simply appened to the end of the URL.

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13738

Make form to submit on action url directly. after click submit you will redirect on http://workflow.abfarmarkazi.ir/phpsql/sms_info.php?id=text_id&mob=text_mob

<form method='get' action ='http://workflow.abfarmarkazi.ir/phpsql/sms_info.php'>
ID:     <input type='text' name='id'><br>
Mobile: <input type='text' name='mob'><br>
<input type='submit' value='submit'>
</form>

Upvotes: 2

r3wt
r3wt

Reputation: 4742

K.I.S.S Keep it simple stupid :-)

<form method='get' action='http://workflow.abfarmarkazi.ir/phpsql/sms_info.php'>
ID:     <input type='text' name='id'><br>
Mobile: <input type='text' name='mob'><br>
<input type='submit' onclick='this.form.submit();'>Submit</input>
</form>

when you click the button it will submit the correct get request to that url.

Upvotes: 0

Ryad
Ryad

Reputation: 31

If your form should be posted to that URL in the onclick Event on the Button, it cannot work, as the $_GET values are not given in the current file.

Just post the form using the action attribute to the sms_info.php and it should work. For example:

<form method='get' action="http://workflow.abfarmarkazi.ir/phpsql/sms_info.php">
ID:     <input type='text' name='code'><br>
Mobile: <input type='text' name='mobile'><br>
<input type="submit" value="SMS">
</form>

In your sms_info.php you can access these values via the $_GET superglobal:

<?php
// file: sms_info.php

$id = $_GET['code'];
$mob = $_GET['mobile'];

// ...
?>

Upvotes: 2

Related Questions