|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How does javascript direct to PHP?
Hii all,
currently, I have a problem to direct my javascript to PHP after checking for data validation. I want to connect to MySQL by using the PHP by using submit() in javascript. I use onclick() in the button in HTML form, and use document.myform.submit() in javascript to acess the database connect, but it cannot. The code as following: <head> <script language="JavaScript" type="text/JavaScript"> <!-- function checkform() { team1 = document.gameform.team1tf.value; if (team1 == "") { alert("Enter the first team of Game "); document.gameform.team1tf.focus(); return; } document.gameform.submit(); } </head> <body> <? if(isset($submit)){ $db= mysql_connect("localhost","root",""); $selectdb = mysql_select_db("one2soccer1"); $query = "select tipserial1 from game where gamename= \"\"order by tipserial1"; $result = mysql_query($query); } ?> . . <form name="gameform" method="post" action="<?PHP_SELF?>"> <input type="button" name="submit" value="Submit" onClick="checkform()"> <input type="reset" name="reset" value="Reset"> </form> </body> Thank you for your information. Regards, Chupiter |
|
#2
|
|||
|
|||
|
I think the problem is here. Change this:
PHP Code:
to this: PHP Code:
Try if that works. |
|
#3
|
||||
|
||||
|
As an added note, I'd like to highly discourage the use of this line: $db= mysql_connect("localhost","root","");
NEVER use root unless for serious administation purposes... 90% of the time, you should be using a different, and limited account... ALWAYS have passwords set for every account AVOID posting the connectivity information in a public forum [heck, even a private forum]... I'd recommend you create a user and grant them fairly limited permissions... if your web program simply reads from a database, only give them permissions to read that specific database... if you need to insert information into a table, give permissions to that specific table... it never hurts to be too cautious of security threats, and always prevent them in as many locations as possible... |
|
#4
|
|||
|
|||
|
Quote:
------------------------------------------------------------ Hi pocketsize, Thanks for ur solution, but your suggestion doesn;t work in the script, there is an error occured. Regards, Piter |
|
#5
|
|||
|
|||
|
Hi madcowzz,
Thanks for you guideance, I will work on that Regards, piter |
|
#6
|
|||
|
|||
|
Strangely enough, I couldn't get the JS submit () function to work eventhough I'm sure I've used it before. Anyway, I changed some of the code. Your form should look like this:
Code:
<form name="gameform" method="post" action="<?PHP_SELF?>" onsubmit="return checkform()"> <input type="submit" name="submit" value="Submit"> <input type="reset" name="reset" value="Reset"> </form> Note the changes: 1) The submit buttons is now a real submit button (type="submit") 2) The submit button no longer has the onclick eventhandler 3) The form has an onsubmit eventhanlder. The JS should look like this: Code:
function checkform() {
team1 = document.gameform.team1tf.value;
if (team1 == "") {
alert("Enter the first team of Game ");
document.gameform.team1tf.focus();
return false; }
return true;
}
I've tested it an it works on my end. |
|
#7
|
|||
|
|||
|
Hii pocketsized,
I ve tried to follow 3 changes ur guide. If I used <?PHP_SELF?>, after press the submit button and correct data validation in javascript, then the browser cannot find the page. Therefore I refer to the page itself in the action="gameidentify.php". However, I try to print alert("") in after the submission, but there is nothing happened. <? //used this command to direct the javascript to PHP database,right? if(isset ($_POST("Submit"))){ print("<script>alert(\"testing completed\")</script>"); } ?> after I set return true, if the data validation in java script is correct. Regards Piter |
|
#8
|
|||
|
|||
|
<?PHP_SELF?> is actually from your original code. Come to think of it, it should look like this <? $_SERVER ["PHP_SELF"] ?>
Anyway, the problem is that php is case-sensitive. You're checking for $_POST ["Submit"] with a capital S when the HTML doesn't have a form element with that name. Change it to $_POST ["submit"] with a lower-case s. Code:
<input type="submit" name="submit" value="Submit"> Notice that the name attribute is spelled "submit" with a lower-case s. |
|
#9
|
|||
|
|||
|
yap.....it works pocketsized........u re my man......but there is slightly a problem I encounter....
in my testing script: I put if(isset ($_POST("submit"))){ print("<script>alert(\"testing completed\")</script>"); } After the data validation has completed, then it will promt the alert message in javascript. However, if I press refresh button without input any value in the forms(textfield,etc), the program will prompt again the alert message without go to my javascript for data validation because there is a value in $_POST("submit"). So is there any way to clear all the value in $variable after the first process is completed. Thank you. Regards, piter |
|
#10
|
|||
|
|||
|
Quote:
Well, after form submission, you could re-direct the user to another page where you display the JS alert. |
|
#11
|
|||
|
|||
|
oke I get it, thanks
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > How does javascript direct to PHP? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|