שלום.
יצרתי טופס הרשמה, הכל פועל מעולה.
החטתי לשפר אותו לעשות בדיקה בJS.
הפונקציה פועלת על ידי onsubmit="return validate();
עכשיו בקוד הישן עשיתי בדיקה אם הכפתור submit נלחץ תכניס את הפרטים, כרגע אני לא יכולה לעשות את זה, כי אני צריך לוודאות שאין בעיות בטופס והכל true.
לצורך הבנה ניד וזהו הדף שלי:
PHP קוד:
<html>
<head>
<title>Untitiled</title>
<script>
function validate()
{
document.getElementById('first_name_error').innerHTML = "";
document.getElementById('last_name_error').innerHTML = "";
var val = true;
var fName = document.frm.first_name.value;
if(fName.length < 3)
{
document.getElementById('first_name_error').innerHTML = "Enter first name please.";
document.frm.first_name.style.border = "red 1px solid";
val = false;
}
var lName = document.frm.last_name.value;
if(lName.length < 3)
{
document.getElementById('last_name_error').innerHTML = "Enter last name please.";
document.frm.last_name.style.border = "red 1px solid";
val = false;
}
return val;
}
</script>
</head>
<body>
<form name="frm" id="frm" onsubmit="return validate();">
<table id="registertable" cellpadding="10">
<tr>
<td align="left">
<span class="field">First name:</span>
</td>
<td align="left">
<input type="text" name="first_name" id="first_name" />
<div id="first_name_error" style="display:inline; font-family:arial; color:red;"></div>
</td>
</tr>
<tr>
<td align="left">
<span class="field">Last name:</span>
</td>
<td align="left">
<input type="text" name="last_name" id="last_name">
<div id="last_name_error" style="display:inline; font-family:arial; color:red;"></div>
</td>
</tr>
<td></td>
<td><input type="submit" value="Submit" />
<input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("phpmyadmin");
$first_name=$_POST['user_name'];
$last_name=$_POST['last_name'];
mysql_query("INSERT INTO users (first_name, last_name) VALUES ('$first_name', '$last_name')");
echo "נרשמת בהצלחה!";
?>
איך אני מפעיל את הקוד ב php רק במידה ולא נמצאו שגיאות בטופס והמשתנה val האחרון הוא אכן true?
חושב שהבנתם אותי.
תודה רבה ושבת שלום.