Jump to content

Form


mortalc

Recommended Posts

A quick question - will a form always redirect when submitted? Even if action="" (i.e. will it refresh)?Note: I'm not going to ask how to solve my problem if the answer is yes, because I want to solve it myself, unless I really cannot think how to do it.

Link to comment
Share on other sites

Wait, you mean "When it submits, stop it from submitting"? Will I still be able to use $_POST["name"]?I have been told that I cannot mix JavaScript and PHP, otherwise I would set a PHP variable on the click of a button to the current text in an input field.

Link to comment
Share on other sites

yes. onsubmit is an event. with javascript you can override or rewrite the event to do something other than the default if that's what you need to do.You "can" mix javascript and PHP, but it usually involves AJAX in which a javascript makes a request to a PHP script for some data, and then use that data on the page somehow.Other options are to have PHP echo out information when a page is built and attach it to hidden elements on the page, which can be read by javascript. I guess it all boils down what you think they can or can't do together (depending on what you're trying to accomplish).

Link to comment
Share on other sites

Thanks! I will see if I can find a way to override the submitting of the function.If I do, will I still be able to use $_POST["name"], for example?

Link to comment
Share on other sites

Thanks! I will see if I can find a way to override the submitting of the function.If I do, will I still be able to use $_POST["name"], for example?
well, no. that's the point DD is trying to make. If it doesn't submit....well you should be able to make the logical conclusion from there.If for some reason you don't want it submitting (for whatever reason) you could write a javascript function to take all the form values and then do something with them... but then why did you stop the form submission in the first place... right? Typically this is just done to pre check all the inputs, with successful validation return true as a result and then the form submits anyway.I guess you could just tell us what you're trying to do..... :)
Link to comment
Share on other sites

To answer the original question (in case it wasn't clear):"A quick question - will a form always redirect when submitted? Even if action="" (i.e. will it refresh)?"Yes. When a form is submitted a new document will be loaded. In order to refresh (or submit) a page without reloading a new one, you'd need to use AJAX.

Link to comment
Share on other sites

I'm always afraid of telling people what I'm doing in case they tell me that my idea isn't a good one, and that I should do it in an utterly different way.Anyway, I'm trying to write a chat function. I've got it all set up but for a few things.You mentioned form validation. I know you can set a JavaScript variable using PHP:

var x = <?php echo "string" ?>;

But can it work the other way around?

Link to comment
Share on other sites

Can you set a PHP variable using JavaScript? Not really. JavaScript executes in the browser, and all it can affect is the document currently in the browser. PHP executes on your server. A brick wall of time and space lies between them.However, you can use AJAX to send data to your server, execute a PHP script, and receive data in response. All this happens without your document reloading or being replaced. I'm starting to think shadow was right, and that this is the tool you're looking for. Check out an AJAX tutorial.

Link to comment
Share on other sites

I am using AJAX in my program. The trouble is, as far as I can tell, it can only read from a file (which is what I am using it for), and not write to one.

Link to comment
Share on other sites

I just want to write some text to a file. PHP would be absolutely fine, in fact it is perfect (for other reasons), but for the fact that I cannot set the text inside a textfield to a variable.Another minor question - are PHP cookies the same as JavaScript cookies?

Link to comment
Share on other sites

Another minor question - are PHP cookies the same as JavaScript cookies?
cookies are same in all case. difference is both languages set and retreive in different ways.
Link to comment
Share on other sites

but for the fact that I cannot set the text inside a textfield to a variable.
I don't understand. Do you mean that you want to get text inside a text area/input and assign it to a variable? At what stage of the process?All of your posts are so short that it is hard to know how to help you.
Link to comment
Share on other sites

I think he meant that he does not know how to update the chat window with the new message when a user clicks send. Right? You can just do that in JavaScript, where you take the value of the textarea in which the user types his/her message and append it to the "conversation" window's innerHTML (or value if you're using a textarea).

Link to comment
Share on other sites

I'm sorry for not explaining.This is what I can do:

  • Update the chat window every new post (from any user)

This is what I can't do:

  • Send the reply off to the file on the click of a button (or even better a press of enter), without refreshing the page.

In PHP I know how to send the text but not retrieve it from the textfield.In JavaScript, I know to retrieve the text but not how to send it off.Another thing is that I need to be able to delete the contents of the file before, and 1 second after, the text is sent. I can do this easily in PHP but not in JavaScript.Ok, now you know what I am trying to achieve. If you want further clarificattion:

  • EITHER how to read the text to a variable on the click of a button/enter press in PHP
  • OR both how to send off the text and delete the contents of the file in JavaScript

ED: I've just discovered that if you use the POST version of xmlhttp.open(), you can send a string with .send(). So would, for example, this:

xmlhttp.open("POST", text.txt, true);xmlhttp.send("message");

write the string "message" to text.txt? If so then the problem has been practically solved.

Link to comment
Share on other sites

javascript cant acces your file or database in your server. you need any server side language to communicate with your server.http://w3schools.com/ajax1) you can set a form with a button. where you set a event eg onlick which will trigger a js function.2) in that js function you will send a request (check here) using ajax to your server. where the request will be caught in php file who will process the instruction. adding data to file or database. check herenow for retriveing data from the file or database.1) set a function which will be triggered on a while (defined time) check here2) that function will send a request to a php page. that php page will pull out the data from file or database.3) cactch the response and show it in your chat windows check here

Link to comment
Share on other sites

javascript cant acces your file or database in your server. you need any server side language to communicate with your server.http://w3schools.com/ajax1) you can set a form with a button. where you set a event eg onlick which will trigger a js function.2) in that js function you will send a request (check here) using ajax to your server. where the request will be caught in php file who will process the instruction. adding data to file or database. check herenow for retriveing data from the file or database.1) set a function which will be triggered on a while (defined time) check here2) that function will send a request to a php page. that php page will pull out the data from file or database.3) cactch the response and show it in your chat windows check here
wow simple and good information's very thanks
Link to comment
Share on other sites

Thank you very much, Mr. Bibal.So a PHP file can run even if it isn't the current page?
A PHP script can be run at any time. It just has to be invoked and there are a number of ways to invoke a PHP script, including "opening" one in a browser or sending a request through AJAX. One could also set up cron jobs (or Scheduled Tasks on Windows) to run PHP scripts at certain times without a browser ever being involved. The only thing required to run PHP is a web server.
Link to comment
Share on other sites

Ok, I have tested it and I know that everything is working EXCEPT for the bit which retrieves the text.I have this webpage in an <iframe> on the chat popup screen. It is the part which displays the text. Or should.

<html xmlns="http://www.w3.org/1999/xhtml">	<head>		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>				<script type="text/javascript" >			var Xrequest= new XMLHttpRequest();			function check()			{				var x;				Xrequest.open("GET", "newmsg.txt", true);				Xrequest.send();				if (!(Xrequest.responseText==x))					{						document.write(Xrequest.responseText + "\n\n");					}				x = Xrequest.responseText;				setTimeOut(check(), 1);			}		</script>	</head>	<body>		<script type="text/javascript">			check();		</script>	</body></html>

I've also just discovered something weird. The php wrote to the file once, but hasn't since. Here are the important parts of the webpage which the <iframe> is in:

<html xmlns="http://www.w3.org/1999/xhtml">	<head>		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>		<style type="text/css">			#msg {width:90%}			#post {width:10%}		</style>		<script type="text/javascript" >			var Xsend = new XMLHttpRequest();			function send()			{				var msg = document.getElementById("msg").value;				Xsend.open("GET", "chp.php?msg=" + msg, true);				Xsend.send();			}		</script>	</head>	<body>		<iframe src="chist.xhtml" width="100%" height="90%"></iframe>		<form action="" method="post">			<input id="msg" type="text" name="msg" />			<button id="post" onclick="send()">send</button>		</form>	</body></html>

and here is chp.php:

<?php$msg = "\n".$_GET["msg"];file_put_contents("newmsg.txt", $msg);?>

Could you just check if I have made any errors or misunderstanding. I'd rather if you didn't, say, tell me that using an <iframe> is not a good idea and that there is a better way of doing it. Unless, of course, that a poor choice of method is the very reason that it is going wrong.

Link to comment
Share on other sites

Using an <iframe> is not a good idea. There is a better way of doing it. :) (Just kidding....I really don't know if there is or isn't)Anyway,For starters, sending an HTTP request every millisecond :) is a VERY bad idea. I'm referring to this line:setTimeOut(check(), 1);Change that to:setTimeout(check, 1000);at least if not 2000 or 3000. The time to wait is specified in milliseconds so a value of 1000 will execute the code every second, which is still probably a little excessive.Also notice I removed the parenthesis from the check function. Doing that will send a reference to check function to setTimeout. With the parenthesis you are sending the return value of the check function to setTimeout.(Oh, I also corrected a typo in your setTimeout call: it should be setTimeout instead of setTimeOut)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...