0

Total greenhorn question: I've got a system that asks users to answer a few questions on a simple form (/form). Once they submit their answers, I want another page (/form/custom) to load content depending on their answers ("You might find this interesting: ...").

The form users fill in sends data to a next page in a URL string: website.com/form/custom?name=Michael&question=yes&colour=blue etc.

I've read about the $_GET variable in PHP, but I'm not sure how I would use that to call tailored content. Say if question=yes and colour=blue I want to show content A, C, and D; but if question=no and colour=yellow I want to show content B, E, and F. Any tips on how to code this? Thanks a lot in advance!

albert
  • 13
  • 1
  • 1
    Does this answer your question? [GET URL parameter in PHP](https://stackoverflow.com/questions/5884807/get-url-parameter-in-php) – Chris S. Jul 07 '20 at 10:22

3 Answers3

0

Hello if I understood properly, first on the new page form/custom set variables with data $question = isset($_GET['question]) ? trim($_GET['question']) : ''; do it for all and then with simple if statements load content u want

if ($question === 'yes' && $color === 'blue') {
    //showContent(A,B, D)
} else if ($question === 'no' && $color === 'yellow') {
   //ShowContent(C,E, F)
}
Plamen Penchev
  • 357
  • 1
  • 5
  • 15
  • Thanks a lot for this! Very helpful. For the lines where I show content do I just insert my usual HTML? Sorry for the basic question, haven't worked with PHP in a while. – albert Jul 07 '20 at 09:06
0
$question = isset($_GET['question] ? trim($_GET['question']) : NULL;
$colour   = isset($_GET['colour] ? trim($_GET['colour']) : NULL;

Now you have checked if the colour and question is passed through the URL and if the colour and question is not passed you have set the default value as NULL.

if($question !== NULL  && $colour !== NULL){
  if ($question === 'yes' && $colour   === 'blue') {
    // logic to show content(A,B, D)
  } else if ($question === 'no' && $colour   === 'yellow') {
   //logic to show content(C,E, F)
  }
}

here i'm using === to check the variable values inside if conditions since it will return TRUE if, for ex: (consider first check in the first if())

$question is equal to 'yes', and they are of the same type.

You can learn more about passing variables with data between pages using URL from here

Hirumina
  • 738
  • 1
  • 9
  • 23
  • IF this helped you to solve you issue accepting my answer would be highly appreciated since it will help me to increase my reputation score. – Hirumina Jul 07 '20 at 09:14
  • To then load the content I want (e.g. A, B, D) do I insert HTML there? – albert Jul 07 '20 at 09:15
  • what does A,B,D means? is it a button or is it a function that needs to executed.or do you just want to print A,B,D – Hirumina Jul 07 '20 at 09:16
  • If it is something to do with the fron end side you will need html inside `if ()`.For ex: if you want to display images as A,B,D you have to add your html cade inside `if()` (because $question = 'yes' and $colour = 'blue'). – Hirumina Jul 07 '20 at 09:22
  • 1
    Perfect that's what I needed to know thanks! I just need to load a set of links to other pages that will be of interest to the respective customer category/combination of responses. So then I'll just add HTML inside the IF function that brings up the content I want in each case. – albert Jul 07 '20 at 11:06
  • @albert yes!! Glad i could help – Hirumina Jul 07 '20 at 11:08
-1
if (isset($_GET['question'])) {
    if ($_GET['question'] == 'A') {
        //load A content
    } elseif ($_GET['question'] == 'B') {
    
    //load B content
    } else {
        //load C content
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    He doesn't need to load the contents separately. And he needs to check the `colour` as well – Hirumina Jul 07 '20 at 10:26
  • 2
    Please add an explanation, highlighting parts of your answer that addresses OP's issue, and why/how. Code only answers are generally frowned upon on SO . Adding details adds to long term value, and helps future visitors learn, so they can apply this knowledge to their own coding issues. Also, it improves quality of the answer, and hence SO as well. Higher quality answers are upvoted more often, as more users find them of greater use as a resource to refer back to. Consider editing to provide additional info, or explain your logic here. – SherylHohman Jul 07 '20 at 14:21
  • Thanks for your contribution. However, this reads more like a suggestion than an Answer. What is shown is simply an example of generic logic, but does not answer the specific question proposed by OP. If is fine to propose suggestions, or generic code, or links as a *Comments*. But *Answers* must contain the specific code necessary to completely, fully, answer the OP's Q, and it must be self-contained. – SherylHohman Jul 07 '20 at 14:32
  • Require support cannot comment due t reputation newbie on stack. I will be sure in future i will explain what i do and give them the correct answer. Thank for your support. @Hirumina – Sohail Farooq Jul 08 '20 at 05:42