Jump to content

How to pass php variables without specifying in the header location


palash4003

Recommended Posts

I have a form, where I'm collecting registration info - like name, address, email, password. I'm calling another php file at server side code, where it will check whether the email exists or not. Pretty simple, just receiving those post variables and check with the database.

 

The issue, if I find a match, I want to go back to the registration.php page, using the following code:

 

if ($email_already_use == "y") {

      $_SESSION['ERROR'] = "Error";

      $_SESSION['MESSAGE'] = "This email address already used. Please try another";

      header("location: ".$settings['site_url']."registration.php");

      exit;

}

 

Now, I would like to place the user input at the registration.php file, but I do not want to use pass variable at URL like this:

 

header("location: ".$settings['site_url']."registration.php?".name=$name&address=$address&email=$email;

 

Is there anyway, I can retrieve those variables at registration.php, without specifying .name=$name&address=$address&email=$email ?

 

Thanks for your help. I see some sites has similar implementation, but did not find anything like how to do it.

 

Link to comment
Share on other sites

Hi SergeiSS,

 

Thanks for your prompt reply with the page link. There is only one issue here:

 

If I go to the registration page first time, it is all these messages - like "Name is invalid" whatever I put at echo message. It's only I want when user click on the submit button. By the way, I have a image button, instead of submit like type="image"

 

<?php

 

$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : "";

if ($name == "") {

  echo "Name is invalid<br>";

}

?>

 

Any more help? I appreciate.

 

 

Link to comment
Share on other sites

You need to check whether the form has been submitted before validating the fields in it. Additionally, you'd be best to store errors in an array for display, rather than echoing them randomly in the code.

 

if( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) {
     // form has been submitted, validate fields
}

Link to comment
Share on other sites

In addition to Pikachu's answer. Another possibility to check if the form was submitted is to check POST array, look for the name of your submit button.

 

For example, you have submit with the name "ok".

if( isset( $_POST['ok'] ) )
{
// do whatever you wish, this block is processed if OK button is pressed
}

 

 

Moreover, you may have more than 1 submit in one form and you may detect the pressed button.

Let's assume that your form contains these 2 submits

<input type="submit" name="ok" value="Save" />
<input type="submit" name="del" value="Delete" />

 

Inside processing part your may write

 

if( isset( $_POST['ok']) ) // ok is pressed
{
  // do whatever when Save is pressed
}
elseif( isset( $_POST['del'] ) ) // del is pressed
{
  // write a code for Delete processing
}

 

Hope it helps :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.