Welcome Guest, Not a member yet? Register   Sign In
Redirect while passing data
#1

[eluser]Freewolf[/eluser]
Greetings all,

I am setting up a script that validates an XML file. I want to redirect the URI to a success or failure value.

However if it is a failure value I do want to print out what the errors where in the validation. What I currently have is.

Code:
public function file_validate()
{
  $file = $_FILES['file']['tmp_name'];
  $this->data['displayMessages'] = $this->leads_model->validateXMLFile($file);
  $isValid = $this->leads_model->isValidXML($file);

  if ($isValid == TRUE) {
   redirect('/leads/success', 'location');
  } else {
   redirect('/leads/failure', 'location');
  }
}

public function success()
{
  $this->output->set_status_header('202');
  echo "Success";
}

public function failure()
{
  $this->output->set_status_header('406');
  echo "Failure";
}

Basically I want to be able to read
Code:
$this->data['displayMessages']
in the failure() method. How would I go about doing that and or rewritting my code??
#2

[eluser]CroNiX[/eluser]
Do you actually need to redirect? It's the same controller so you should just be able to call that method.
instead of
Code:
redirect('/leads/success', 'location');
try
Code:
$this->success();
and change the other one too

As far as accessing that data in the failure method...
do it as you basically showed
instead of
Code:
echo "Failure";
try
Code:
echo $this->data['displayMessages'];

But you obviously can't do that using a redirect as you lose the contents of $this->data, unless you reload it somehow (or stick it in a session variable which you then can read in the failure() method).

You don't have to worry about that though if you did it as I showed you as you never leave the controller so all data should still exist.
#3

[eluser]Freewolf[/eluser]
If am not sure if I need the redirect, but I do what the URI to change. Is there a way to go about accessing the method within AND change the URI?

Basically I want to submit the file and if the file is valid
someurl.com/leads/success

If the file is not valid then
someurl.com/leads/failure
#4

[eluser]CroNiX[/eluser]
If you want the url to change then yes, you do need a redirect. But then you have more work to do with storing your error message in session just before you
Code:
redirect('/leads/failure', 'location');
and then retrieving it in your failure() method so you can display it.

You could also use a simple cookie for that.
#5

[eluser]xerobytez[/eluser]
As CroNIX said in order to preserve data like that between requests you will have to store it somewhere. Can be in a cookie, the session or even the database. The other possibility that I personally wouldn't recommend would be to pass the message through a variable in the URL. so you could do something like this.

Code:
redirect('/leads/failure/' . $message, 'location');

Then you could access it like this.
Code:
function failure($message = '') {
    echo $message;
}

This could be considered bad practice as its very transparent, leaves it open for your users to mess with and can make the URL look unclean. Just letting you know its possible.




Theme © iAndrew 2016 - Forum software by © MyBB