Jump to content

display different code depending on the title/url


Bentley4

Recommended Posts

Hi guys!

 

How do I display different code depending on the title/url?

This is the code for the form that brings you from

"http://www.xxx.uk/index.php" to "http://www.xxx.uk/index.php?name=ro":

<form name="input" action="http://www.xxx.uk/index.php?name=ro" method="post">
Enter student number:<input type="text" name="sid"/>
<input type="submit" value="Submit">

 

Now when you are on "http://www.xxx.uk/index.php?name=ro" I would like another content(but written in the same php file).

I tried with _GET, but the problem is that I don't simply want to add a word but a lot of content.

With _GET, that whole content would have to be displayed in the title and I don't want that.

 

 

Link to comment
Share on other sites

Thnx violinrocker!

 

But when I use this code I get a parse error.

The code:

 

<?php
if ($_GET=='') echo{ 
<form name="input" action="http://www.xxx.uk/index.php?name=ro" method="post">
Enter student number:<input type="text" name="sid"/>
<input type="submit" value="Submit">"
</form>;}
else if ($_GET=='ro') echo {"the really long content that you want in here";}
?>

 

I can't find where the syntax error is, can anybody see where?

Link to comment
Share on other sites

Your code is not syntactically valid at all. All text/html you want to be outputted should be wrapped in quotes not curly braces as violinrocker demostrated.

<?php
if ($_GET=='')
{ 
    echo '<form name="input" action="http://www.xxx.uk/index.php?name=ro" method="post">
Enter student number:<input type="text" name="sid"/>
<input type="submit" value="Submit">"
</form>';
}
elseif ($_GET=='ro')
{
    echo "the really long content that you want in here";
}
?>

Link to comment
Share on other sites

Wouldn't the following be the correct way to do it?

ATM you are only trying to compare $_GET and not the actual variable $_GET['name'].

 

<?PHP

  if(isSet($_GET['name']) && $_GET['name'] == 'ro') {
  
    echo 'Long Content Here.';

  } else {

    echo '<form name="input" action="http://www.xxx.uk/index.php?name=ro" method="POST">',
         'Enter student number: <input type="text" name="sid"/>',
         '<input type="submit" value="Submit">',
         '</form>';

  }
?>

 

Try the above code and tell me how it goes :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

@Wildteen88

Thanks, this works!!

But actually my code is still a bit longer, I left a bit out for the sake of brevity.

When I include this bit it still gives an error though. Can anyone see why?

The extra code is written in bold(without the code written in bold it works iow)

 

<?php
if ($_GET=='') echo 
'<form name="input" action="http://www.xxx.uk/index.php?name=ro" method="post">
Enter student number:<input type="text" name="sid"/>
<input type="submit" value="Submit">

//Why is the following between the php tags giving an error?
<?php
$name = $_POST['sid'];
$timein = time();

$fp = fopen("formdata.txt", "a");
$savestring = $name . "," . $timein . "\n";
fwrite($fp, $savestring);
fclose($fp);
?>

</form>;'
else if ($_GET=='ro') echo "the really long content that you want in here";

?>

 

 

@PaulRyan

I first want this thing to work and then I'll get to your comment

Thanks in advance : )

 

Link to comment
Share on other sites

That code won't work as you are trying to echo out the PHP code between the extra PHP tags, remove the tags and then put that code after OR before the echo-ing line :)

 

Like this

<?PHP

  if(isSet($_GET['name']) && $_GET['name'] == 'ro') {
  
    echo 'Long Content Here.';

  } else {

    if(isSet($_POST['sid'])) {
      $name = $_POST['sid'];
      $timein = time();

      $fp = fopen("formdata.txt", "a");
      $savestring = $name . "," . $timein . "\n";
      fwrite($fp, $savestring);
      fclose($fp);
    }

    echo '<form name="input" action="http://www.xxx.uk/index.php?name=ro" method="POST">',
         'Enter student number: <input type="text" name="sid"/>',
         '<input type="submit" value="Submit">',
         '</form>';

  }
?>

 

That will work in theory, but is untested :)

 

I've added an if statement, to only add the text to the file if the form was actually posted.

Anything goes wrong, gimme a shout.

 

Regards, PaulRyan,

Link to comment
Share on other sites

Thanks Paulryan!!

 

I've first tried to adjust the code that I was working on and posted the bit before the echo statement and it doesnt give any parsing errors. Thanks for that. You were right, this code doesn't show any submit field and button.

 

I've tried your code, it doesnt give any parsing errors, it also shows the submit button and field

but when I fill in something and click submit it doesn't send this data to the textfile anymore.

Any idea why?

Link to comment
Share on other sites

My fault entirely, I forgot to read the forms action URL, they the following.

 


<?PHP

  if(isSet($_GET['name']) && $_GET['name'] == 'ro') {
  
    if(isSet($_POST['sid'])) {
      $name = $_POST['sid'];
      $timein = time();

      $fp = fopen("formdata.txt", "a");
      $savestring = $name . "," . $timein . "\n";
      fwrite($fp, $savestring);
      fclose($fp);
    } else {
      echo 'Form not posted.';
    }

  } else {

    echo '<form name="input" action="http://www.xxx.uk/index.php?name=ro" method="POST">',
         'Enter student number: <input type="text" name="sid"/>',
         '<input type="submit" value="Submit">',
         '</form>';

  }
?>

 

That should work now :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

Hey Paulryan,

 

Thnx! No parsing errors and it sends the data to the textfile!

But there is still something wrong:

The first else statement can't be triggered! When I don't fill in anything and click submit, it doesn't work different then filling in something and clicking on submit. I don't get the notice "Form not posted."

Also, where should I put: 

echo 'Long Content Here.';

Link to comment
Share on other sites

Well that means you'll have to do some error checking when posting the form, surely you can do that? :)

Can't expect us to do everything now can you?

 

My simple error checking if(isSet($_POST['sid'])) is only to check for submission, if you just visit that page with ?name=ro at the end of the URL it will show Form Not Posted.

 

Put the

 echo 'Long content here'; 

in place of

 echo 'Form Not Posted.'; 

 

So it will show the long content if the form has not been submitted.

 

Try it an sort some error checking out and see what you get, if you're still struggling, come back and ask :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

K, Thnx for the advice Paulryan.

I fiddled around with it and adjusted it.

 

New problem(something I can't easily figure out myself):

After the data is submitted,you would see again a form(with a question). (And the same code again with adjusted content)

I would like to implement at least 10 questions like this.

Problem: I would have to nest each code of the page into the echo statement again.

It becomes unreadable!

 

Isn't there another way to have the same funtionality without nesting?

I'm thinking something with a counter, but I have no idea where to start. What do you think?

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.