Bentley4 Posted May 7, 2011 Share Posted May 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/ Share on other sites More sharing options...
violinrocker Posted May 7, 2011 Share Posted May 7, 2011 try if ($_GET=='') { echo "the form in here"; } else if ($_GET=='ro') { echo "the really long content that you want in here"; } Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1211905 Share on other sites More sharing options...
Bentley4 Posted May 7, 2011 Author Share Posted May 7, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1211913 Share on other sites More sharing options...
wildteen88 Posted May 7, 2011 Share Posted May 7, 2011 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1211924 Share on other sites More sharing options...
PaulRyan Posted May 7, 2011 Share Posted May 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1211936 Share on other sites More sharing options...
Bentley4 Posted May 8, 2011 Author Share Posted May 8, 2011 @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 : ) Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1212291 Share on other sites More sharing options...
PaulRyan Posted May 8, 2011 Share Posted May 8, 2011 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, Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1212295 Share on other sites More sharing options...
Bentley4 Posted May 8, 2011 Author Share Posted May 8, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1212299 Share on other sites More sharing options...
PaulRyan Posted May 8, 2011 Share Posted May 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1212304 Share on other sites More sharing options...
Bentley4 Posted May 8, 2011 Author Share Posted May 8, 2011 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.'; Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1212309 Share on other sites More sharing options...
PaulRyan Posted May 8, 2011 Share Posted May 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1212313 Share on other sites More sharing options...
Bentley4 Posted May 8, 2011 Author Share Posted May 8, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235768-display-different-code-depending-on-the-titleurl/#findComment-1212321 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.