Jump to content

getting echo to appear at a particular place


Strac72

Recommended Posts

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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. :)

Link to comment
Share on other sites

You uncommented out your code, right? :P

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!  8)

 

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.

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.