...

View Full Version : Two languages for one form...



wylie233
08-05-2012, 09:41 PM
Hello All.

I am making a project that involves PHP and Javascript. I have one text field and a button that uses 'onclick = "go()", only today do i want to at PHP
in to my program, i have changed my file to a .php and ready, i have apache and all that and know how to use PHP and have successfully used it before!
So, i open my project through localhost, still no PHP code yet. works fine. soon i open php tags (<?php xxxx?>).........works fine. now, i then wanted to use
the '$_POST' function to get the text field, the text field and button are not in a form, but i know for PHP i need it to be in a form, so i open form tags with a method of POST, give the texfield a name not just a id (PHP needs a name),
this is when the javascript goes weird and the PHP is working, so lets say i type 'hello' it is meant to return hi (thats made in JS), but returns hi for one second before redirecting the page to the same page (because its now a form)........So, my problem as you can see is php works but when i print from javascript (.innerHTML) it shows for a second before the action of the form reloads the page. Sorry id i have not explained it very well, please ask if i am not giving enough info, Many Thanks to anyone who can help me out, that would be highly appreciated!!!!

AndrewGSW
08-05-2012, 09:59 PM
We would need to see some code, but my psychic powers :cool: tell me that you're typing 'hello' and pressing the Enter key - which causes the form submission.

wylie233
08-05-2012, 11:01 PM
<?php





$textfield = $_POST['textfield'];
if (isset($textfield) &&!empty($textfield)){


if(preg_match('/yes/' , $textfield)){


echo 'OK';

}
}



?>




<html>

<head>

<script>


function go(){

if (document.getElementById("textfield").value.substring(0, 2) === "ok"){

body2.innerHTML = "yes";

}


};
</script>
</head>

<body>



<form action = "untitled.php" method = "POST">

<input type = "text" name = "textfield" id = "textfield"><br>


<input type = "submit" onclick = "go()">

</form>



<p id = "body2"></p>




ok so theres the code, when you type 'ok' javascript will print 'yes' for as long until it reloads, PHP works when you type 'yes' it will print 'OK' and stay there, what i want is both to stay there!!!! Thanks

DrDOS
08-05-2012, 11:56 PM
You can return data you submitted in your form to the same page by echoing it into the html or the javascript. You need an if( !isset ( not isset ) statement to provide any default values to use before the form is posted, otherwise you get errors.

wylie233
08-06-2012, 12:36 AM
Ok, i don't quite understand (sorry), my problem is the javascript prints out data before the page reloads, this leads to the string it did print go again in seconds, but the php prints it on the page once its loaded, so it remains showing......why can't i do this in JS??

thanks

AndrewGSW
08-06-2012, 02:03 AM
<input type = "submit" onclick = "go()">

This submit button will run your JS go() and then immediately submit the form.. that is the purpose of this button ;)

You could have a separate button to run your JS


<button id="justJS" onclick="go()">Go!</button>

Alternatively,


<form action = "untitled.php" method = "POST" onsubmit="go()">

will run your go() function on clicking the submit button, and will NOT submit if your function has 'return false;'.

Added: To complete this list of choices, you could remove the submit button and use just


<button id="justJS" onclick="go()">Go!</button>

and this go() function could, optionally, use


document.getElementById('yourformsid').submit();

to submit the form.

DrDOS
08-06-2012, 02:31 AM
It's better to use onmousedown instead of onclick for a submit button, because the form submits onmouseup. That is, if you want to run the javascript before the form submits.

wylie233
08-06-2012, 11:48 AM
Thank you both, so i tried AndrewGSW's options but still 'yes' is only displayed before the page reloads (second), but maybe i mean, after the page reloads, THEN run the go() function, surly then the page would not reset the javascript because the javascript is only there until the page has finished reseting....So what would the code look like for that??


Thanks to both, its really helpful!

AndrewGSW
08-06-2012, 02:38 PM
but maybe i mean, after the page reloads, THEN run the go() function

I think you need to clarify in your own mind what you want to happen.

It is possible to create PHP that causes some JS to run after the page has finished loading, but it is usually not necessary as the PHP itself can, for example, insert the word 'yes' into the page.

But running some JS just prior to submitting/ reloading the page is a little pointless. The only reason to do so is usually to validate, or change, the form data and optionally cancel the submission of the form (if the data is invalid).

wylie233
08-06-2012, 03:58 PM
Ok, the code i posted is the bases of what i am doing, the reason why i want to languages is because i want to use PHP foe setting cookies, anyway,


It is possible to create PHP that causes some JS to run after the page has finished loading


How would i do that, or what is the code for what i need?

Thank you very much!

AndrewGSW
08-06-2012, 04:30 PM
echo "<script type=\"text/javascript\">window.onload=go;</script>;";

wylie233
08-06-2012, 04:57 PM
Sooo:




<?php





$textfield = $_POST['textfield'];
if (isset($textfield) &&!empty($textfield)){


echo "<script type=\"text/javascript\">window.onload=go();</script>";

if(preg_match('/yes/' , $textfield)){


echo 'OK';



}

}


?>




<html>

<head>

<script>

function go(){
if (document.getElementById("textfield").value.substring(0, 2) === "ok"){

body1.innerHTML = "You: " + document.getElementById("textfield").value;
body2.innerHTML = "Paul: " + reply34;

}

}
</script>
</head>

<body>


<form action = "untitled.php" method = "POST" >

<input type = "text" name = "textfield" id = "textfield"><br>

<button id="textfield" onclick="go()">Go!</button>

</form>



<p id = "body2"></p>




Is not working....

AndrewGSW
08-06-2012, 05:43 PM
Somehow in copying my code you've managed to add brackets after the word go.. :confused:

And where is reply34 defined?

wylie233
08-06-2012, 07:49 PM
Correction:



<?php





$textfield = $_POST['textfield'];
if (isset($textfield) &&!empty($textfield)){


echo "<script type=\"text/javascript\">window.onload=go;</script>";

if(preg_match('/yes/' , $textfield)){


echo 'OK';



}

}


?>




<html>

<head>

<script>

function go(){
if (document.getElementById("textfield").value.substring(0, 2) === "ok"){


body2.innerHTML = "YES ";

}

}
</script>
</head>

<body>


<form action = "untitled.php" method = "POST" >

<input type = "text" name = "textfield" id = "textfield"><br>

<button id="textfield" onclick="go()">Go!</button>

</form>



<p id = "body2"></p>



.......Is not working.........


(Sorry about that!!)

AndrewGSW
08-06-2012, 08:42 PM
body2.innerHTML = "YES ";

if you haven't already defined body2 elsewhere then:


document.getElementById('body2').innerHTML = "YES ";

wylie233
08-06-2012, 08:57 PM
<?php





$textfield = $_POST['textfield'];
if (isset($textfield) &&!empty($textfield)){


echo "<script type=\"text/javascript\">window.onload=go;</script>;";

if(preg_match('/yes/' , $textfield)){


echo 'OK';



}

}


?>




<html>

<head>

<script>

function go(){
if (document.getElementById("textfield").value.substring(0, 2) === "ok"){


document.getElementById('body2').innerHTML = "YES ";

}

}
</script>
</head>

<body>


<form action = "untitled.php" method = "POST" >

<input type = "text" name = "textfield" id = "textfield"><br>

<button id="textfield" onclick="go()">Go!</button>

</form>



<p id = "body2"></p>





Just tried it and is still not working (really sorry about this),

is there more code wrong???

or do i need a new method (sorry...)????

BTW, THANK YOU * 1000000

AndrewGSW
08-06-2012, 09:15 PM
I put an extra semi-colon at the end of this (corrected) statement


echo "<script type=\"text/javascript\">window.onload=go;</script>";

but this would not cause it to fail.

But your go() function has this:


if (document.getElementById("textfield").value.substring(0, 2) === "ok") {

When this is run (just after the page has loaded) 'textfield' will not contain the word 'ok' - unless you explicitly put it there from within your PHP code.

I still think you need to clarify, for yourself, how JS and PHP work. They are quite distinct tools. But you seem to be ploughing on regardless ;)

wylie233
08-07-2012, 09:09 AM
Ok, so what are you saying , its impossible?


I still think you need to clarify, for yourself, how JS and PHP work. They are quite distinct tools. But you seem to be ploughing on regardless


So what exactly do you mean.....

AndrewGSW
08-07-2012, 01:29 PM
@wylie233
Never mind, ignore me.

wylie233
08-07-2012, 04:30 PM
Ok.......so....is it possible?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum