Strac72 Posted September 11, 2011 Share Posted September 11, 2011 I have gleaned a script from modified bits of tutorials and ended up with this: <td> <form name="input" action="fotw.php" method="post"> <h4>Want a specific film? <input type="text" width="300" name="requests" /> <input type="submit" value="Request" /><input type="hidden" name="thanks" /></h4> </form> </td> <?php $requests = $_POST['requests']; $data = "$requests\n"; $fh = fopen("../filmoftheweek/users.txt", "a"); fwrite($fh, $data); fclose($fh); /*if (???????? HELP!);*/ // request button has been clicked echo "Thanks, I'll see what I can do."; // In the field I have highlighted in red ?> What I want to do is get the Thanks message to print to the right of the request button but, being a total noob because I didn't pay attention at college during any php bits, I have no idea how to go about it. It's just a visual thing really as I know the text file gets written but, disappearing text does little to reassure the user that something has happened. Probably the field won't be any good as a place to get printed but I tried (for hours) on this. Any help would be greatly appreciated. Along with an explanation, doubly so. Many thanks in advance, Paul Note to moderators: CSS is an acronym for Cascading Style..? Sheet/sheets [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/ Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 You'll need to put the request button HTML below the PHP for starters. Then you can simple open up the php tags inside of the HTML itself and echo it there. http://php.about.com/od/learnphp/qt/php_with_html.htm Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268126 Share on other sites More sharing options...
Strac72 Posted September 11, 2011 Author Share Posted September 11, 2011 Do you mean put the form after the processing? This doesn't make sense to me, sorry. I know you can interchange html and php but I don't see how that would help. Excuse me if I'm being dense. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268133 Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 Nevermind. I get what you mean now. To do that, you'll need to use AJAX. When the user hits the submit button it will send a request to the server and you can store the value of your field in red in a variable, such as: $reply. You're not going to be able to do this with PHP and HTML alone unless you refresh the page. Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268138 Share on other sites More sharing options...
Strac72 Posted September 11, 2011 Author Share Posted September 11, 2011 Hmm, something I know f.a about, any ideas or is it back to scraping through tutorials again? Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268147 Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 <?php if (!empty($_POST)) { // this branch will process the information sent to the server /* Commenting out your custom code, since it won't work for me ;p $requests = $_POST['requests']; $data = "$requests\n"; $fh = fopen("../filmoftheweek/users.txt", "a"); fwrite($fh, $data); fclose($fh); */ $reply = ' Thanks! I\'ll see what I can do!'; } else { // this branch will process front-end code (if you have any) ?> <!-- Dependency --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js" ></script> <?php $reply = ''; } ?> <table> <td> <form name="input" id="fotw" action="fotw.php" method="post"> <h4>Want a specific film? </h4> <input type="text" width="300" name="requests" /> <input type="submit" value="Request" /><span id="ajaxDiv"><?php echo $reply; ?></span> <input type="hidden" name="thanks" /> </form> </td> </table> <script type="text/javascript"> <!-- YAHOO.util.Event.onDOMReady(function() { hijackForm("fotw", "ajaxDiv"); }); function hijackForm(formId, updateId) { var formObj = document.getElementById(formId); // this is the callback for the form's submit event var submitFunc = function (e) { // prevent default form submission YAHOO.util.Event.preventDefault(e); // define a YUI callback object var callback = { success: function(o) { document.getElementById(updateId).innerHTML = o.responseText; }, failure: function(o) { alert("Server request failed. Please try again."); } } // connect to the form and submit it via AJAX YAHOO.util.Connect.setForm(formObj); YAHOO.util.Connect.asyncRequest(formObj.method, formObj.action, callback); } // call our submit function when the submit event occurs YAHOO.util.Event.addListener(formObj, "submit", submitFunc); } --> </script> Works on my localhost. Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268159 Share on other sites More sharing options...
Strac72 Posted September 11, 2011 Author Share Posted September 11, 2011 Ah, top man, that's exactly what I wanted, apart from the fact that although I have the visual function now, even with "my" code back in, it won't write to my text file any more! I won't know what people want! Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268168 Share on other sites More sharing options...
Pandemikk Posted September 12, 2011 Share Posted September 12, 2011 You uncommented out your code, right? The value submitted in the text box is stored in $data which when echoed returns what I typed fine. Although I'm not sure why you're assigning two variables to "$_POST['requests']" or not sanitizing it at all. I don't know why it won't write to your file, try echoing $data in the post branch to make sure there's something there. Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268172 Share on other sites More sharing options...
Strac72 Posted September 12, 2011 Author Share Posted September 12, 2011 I've almost got it working with my old code simply by moving the </td> to the end of the script. Problem is, the text now appears in the right part of the page, but before the request button is even clicked. I'm nodding off here, I'll try and understand what you just said, give me half an hour! Thanks for this. Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268173 Share on other sites More sharing options...
Strac72 Posted September 12, 2011 Author Share Posted September 12, 2011 Wooo Hahhh! Thank you Pandemikk for the if not empty variable. This code works 100%: <td> <form name="input" action="motw.php" method="post"> <h4>Want a specific film? <input type="text" width="300" name="requests" /> <input type="submit" value="Request" /></h4> </form> <?php $requests = $_POST['requests']; $data = "$requests\n"; //open the file and choose the mode $fh = fopen("../filmoftheweek/users.txt", "a"); fwrite($fh, $data); fclose($fh); if (!empty ($_POST)) print "Thanks, I'll see what I can do."; ?> </td> It writes to my txt file again and pops up in the right place after thebutton is clicked! Go to http:strac.me/motw.php and there might be something in it for you if you like films. I'll leave the sesssion start security off until the morning. Top man, and thanks again for making me think harder. Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268179 Share on other sites More sharing options...
Strac72 Posted September 12, 2011 Author Share Posted September 12, 2011 http://strac.me/livingroom/motw.php I said I was tired! Quote Link to comment https://forums.phpfreaks.com/topic/246928-getting-echo-to-appear-at-a-particular-place/#findComment-1268184 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.