header Location and passing multiple variables and receiving them $_GET

I am able to send one variable but I get a weird error when trying it with multiple variables. The code down below I am performing a simple validation before sending the data to be stored in my database. I know I could use JavaScript to do this, but the point is I am trying to learn PHP. When sending one variable, it gives me no errors. God forbid I try and replicate the logic it doesn’t recongize my variables look at look.php
[php]

if(isset($_POST[“EnglishWord”])&&isset($_POST[“JapaneseWord”])){
$English = $_POST[“EnglishWord”];
$JapaneseWord = $_POST[“JapaneseWord”];

if (empty( $English ) || empty( $JapaneseWord ) ) {
echo “It’s empty”;
}else{
echo “worked1”;
header(“Location:lookup.php?data=”.$English.$JapaneseWord);
}

}

[/php]

LOOKUP.php
[php]
$Japaneseword = $_GET [‘JapaneseWord’];
$EnglishWord = $_GET[‘EnglishWord’];
$q = “INSERT INTO JapaneseDefinition(Japaneseword,EnglishWord)VALUES(:JapaneseWord,:EnglishWord)”;
$query = $conn->prepare($q);
$query->execute(array(’:EnglishWord’=>$EnglishWord,":JapaneseWord"=>$Japaneseword));

}catch(PDOException $e){
echo 'ERROR: ’ . $e->getMessage();

[/php]

The error I get is :
Notice: Undefined index: JapaneseWord in C:\wamp\www\lookup.php on line 7

Notice: Undefined index: EnglishWord in C:\wamp\www\lookup.php on line 8

it’s storing null values into my data. I am unable to get the data passed via the url.

You haven’t included the code actually doing the request :wink:

But! You could try debugging it, if you don’t have debugging tools installed just do a var_dump($_POST). The error message clearly states that you don’t have the array keys you are trying to use, so the first thing to check is what data you actually have ^^

If you don’t have anything then debugging the code doing the request is next.

My variables are being set to null when I try to attach more than one variable through the URL. I am able to successfully send one variable and retrieve it in my lookup.php file using $_GET method. The problem i am having is using the header function to properly have it send more than one variable.

This works perfectly. Any modification of this method gives me undesired results. I am unsure on how to properly add another variable and retrieve it using $_GET method. Intuitively, I would think I could just add another question mark make another variable “Japanese” and then using $_GET to retrieve it. Unfortatnetly, this is not the case.

[php]
header(‘Location:lookup.php?English=’.$English);
[/php]

I can retreive the data by using $EnglishWord = $_GET [‘English’];

I have looked around and found that this is the is proper format?
[php]
header(“Location: index.php?variable_1=true&variable_2=false”);
[/php]
When I try to replicate this it just stores the variable as a string into my database. Sigh.

Share the working code. Sounds like you’re on the right track but it’s hard helping if we don’t know how your code looks atm. You should be able to figure this out yourself though by looking at the indeed correct proper format, and var_dumping $_GET/$_POST

I think you have found the solution but what do you mean by:

When I try to replicate this it just stores the variable as a string into my database. Sigh.

Do you want to store the words other than string?

You could do the following:
[php]
$queryString = “?EnglishWord={$English}&JapaneseWord={$JapaneseWord}”;
header(“Location: lookup.php”. $queryString);
[/php]

I think he means that file.php?english=fast?japanese=hayai stores the actual string “fast?japanese=hayai”, which is the expected behaviour. But can’t be sure, just an assumption.

@JimL, thanks. I think so this what he is trying to say.

@louis345. If this is the case just enclose your variables between curly braces {}. Enclosing variables this way within strings ensures that variables are rendered properly.

In this case it would be because he use a question mark to identify each variable, instead of the correct ?var1&var2&var3… ^^

I think this is the problem. Can’t wait till I get home to test it.

I was unsure the proper format to pass many variables via the header location method.

I want to save the contents from the variable into my database not the variable name of the variable. Does that make more sense.

that’s a perfect thing to seach up if you’re not sure. The php manual is a great tool.

it makes perfect sense, but Im having a hard time seeing that it is a problem.

Perhaps you should read up on arrays as well :slight_smile:

[php]

“Location: https://photorankr.com/instagram_fancybox/index.php?code=".$token."&id=”.$userid
[/php]

I used this syntax it worked perfectly. My syntax was done poorly.

Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service