How can I fetch data from a clicked link and insert this into a string?

Basically, I have a script that sends e-mail to a user upon invitation. In this e-mail is a link the invited member must follow to continue. So my question is, how do I add a string to the link to include the user’s e-mail (as gathered by the first script) so when clicked, it goes to the next URL/script and then inserts this data in the $email field on the second script?

$link = $base.$useremail;

$emailfield = $_GET['whateverkeyyouusedfortheuseremailintheurl'];

Okay I got the e-mail address inserted into the link URL, but I’m not sure about the second part. Here’s a code snippet:

$link = $base.$email;

Then later:

https://xxxxxx.net/?$link

Which is e-mailed to the invitee. But when they click it, how do I get the second script to read it and insert the value into the e-mail form field?

I tried this in my second script’s form right above the e-mail input field:

<?php $email = $_GET['$link']; ?>

But I don’t think it will work…

you could read that up the manual:

https://www.php.net/manual/en/reserved.variables.get.php

1 Like

Thanks for the link! It got farther now. :smiley: I now have this in my code on the second script just inside the form with the intent to simply display to the user the e-mail that was passed when they clicked the link:

<?php $email = ($_GET['$mail']); ?>
<?php echo $email; ?>

And:

$link = $base.$email;

https://xxxxxxx.net/forum?mail=$link

In the first script as part of the e-mail it sends. It doesn’t work. :frowning:

You don’t need the $ when accessing the array. If the variable name in the URL is “mail” you can just use $_GET[“mail”] to access it. You can look at the array contents with var_dump($_GET);, so you see all the array keys you can use.

1 Like

I would personally encode the variable value, then on the page the link directs to, decode it.

$url = 'example.com';
$link = $url . '?mail=' . base64_encode($e);
echo $link;

Sounds cool. How exactly do I implement this?

Thanks, got it working. :slight_smile:

@astonecipher Also like your idea but need help with it. :slight_smile:

Is there any way to “remember” the state of “mail”? It works, and I have it set to automatically insert the value into the e-mail form text input, but if I hit “Send” and there is any error in the form, it displays the error but then erases the e-mail from the input field. How can I fix this? Thanks!

When you submit the form you must have this value anywhere to display it, so where does it get lost? When you hit the back-button in your browser you should get on that page that contains the mail within the URL.

The form gets processed in the same PHP file as the HTML and display errors in-line. When someone clicks the e-mail link containing the $GET string, it proceeds to the URL, and then automatically inserts the data via the “value” attribute in the form e-mail input code. But when you click Submit, and have any errors, it “refreshes”, and the $GET string vanishes from the e-mail input field. Is this normal? I couldn’t find any way around this…

If you refresh the page, you should redirect to the same URL you came from. But if you just display some other content, the mail must still be somewhere, you just have to switch from the $_GET to $_POST or however you process the mail further.

I don’t understand. :frowning: Here’s a snippet with the GET code:

if (empty($username)) {
    	  $name_error = "<br><span class='error_msg'>Username cannot be blank!</span>";
  	}else if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL, FILTER_NULL_ON_FAILURE)) {
 	  $email_error = "<br><span class='error_msg'>Invalid e-mail!</span>";      	  
       // this is supposed to catch an invalid e-mail, and I added the below but it doesn't work
	  $mail = $_GET['email2'];
	}else if(strlen($password) < 8) {
	  $pass_error1 = "<br><span class='error_msg'>Password needs to be at least 8 characters!</span>";
	}else if($password != $password2) {
        $pass_error2 = "<br><span class='error_msg'>Passwords do not match!</span>";
	}else if (empty($who)) {
    	  $who_error = "<br><span class='error_msg'>Who referred you??</span>";
  	}else{

And the form code:

<?php $mail = $_GET['email2']; ?>
<center>
<section>
<div class="footernewback"><img src="/images/ts2.png"><br>
<img src="/images/cooltext199737635737939.png"><br>
<form method="post" action="index.php" id="register_form">
<img src="/images/cooltext204092412607586.png"><br>
<div <?php if (isset($email_error)): ?> class="form_error" <?php endif ?> >
<input type="text" name="email" size="35" value="<?php echo $mail; ?>" autofocus />
<?php if (isset($email_error)): ?>
<span><?php echo $email_error; ?></span>
<br>
<?php endif ?>
</div>

Can you make any sense of it? :smile:

not really, once you are using “email” as the key to verify, then you are referring to “email2” when accessing the value. The workflow i would expect is

Link => PHP file => value in $_GET => insert into form, submit => PHP file => value in $_POST

even that you could just set the email in the URL of the action-attribute on your form, so you have this value consistent in $_GET.

The key is email2 and I made it = $mail, and the form string that the user enters is $email, unless $mail is set, in which case it fills it in with $mail via echo. But back to the same problem, upon page refresh or displaying of errors, the value then vanishes from the input field whenever you click submit.

Ideally, I’d like $mail to be returned to the email input field if submit is clicked and there are errors.

*Update, I see what’s happening (maybe), when the page refreshes upon errors, the URL loses the “?email2” key and returns to just a normal URL. Could this be the issue?

if you don’t switch between $_GET and $_POST as long as you provide it once via URL ($_GET) and once via form ($_POST), then yes. But as i already mentioned:

Got it:

action="index.php?email2=<?php echo $mail; ?>"

:smiley: Thanks!

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service