I'm collecting an argument from a signup URL using '?' like this,
.../signup.php?id=6489
now i'm collecting this 'id' in my php in this way,
<?php
$id = $_GET['id'];
// echo $id;
?>
Now i want to use this $id in another PHP file - code_exec.php where there is a mail() function which emails this and other parameters collected from a form. Now the parameters collected from the Form work great and they all show properly in the email. However this ID variable does not. I have included the signup.php in code_exec.php using this,
include 'signup.php';
and yet it doesn't seem to work. What am I doing wrong here?
Without showing what's in signup.php and how you're using that $id value, we can't help. But the obvious one: If the signup.php stuff is working inside a function, then you're subject to PHP's scoping rules and will have to make $id
a global, eg.
$id = $_GET['id'];
function email() {
global $id;
blah blah blah
}
// foo.php
$id = '12345';
include 'bar.php';
// bar.php
echo $id;
Will output 12345. Any variables that exist in scope of the include/require call will be available in the included file.
Add a hidden input value to your form and assign it the passed id value, something like this:
<input type="hidden" id="id" name="id" value="<?php echo $id ?>" />
From what it sounds like, you go to signup.php?id=6489
, fill out a form which submits to code_exec.php
and you want to get the ID in code_exec.php
.
If that is the case, you can include a hidden form field in the signup page with that ID.
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
and that will get submitted with the form just like all of the other fields. Make sure to sanitize your inputs!