PDA

View Full Version : What is wrong with this ASP code? (WXP)



rsasalm
01-29-2003, 03:02 PM
Hi all

I have some problem with ASP script.
If someone can help me to point out what I am doing wrong.

Below you can see the code.

It works as follow:
When one click on "Add a customer" button with all fields filled with text, alert
dialogs appear with the text filled in the field and it seems to work fine.
Then I try to open a mySQL database and insert a row with data.
But the alert dialog with text "New customer inserted" never pops up which means
that there is some syntax problem with the statement to open and insert inta
the database. Kan some trace out what is wrong with these line of code.

I have one more question:
As you see that my "myfunction" is written and called in Javascript language.
What changes do I need if the same code for "myfunction" is written in VB and
how to call when "Add a customer" is pressed?



Thanks in advance for your help

regards
/rsasalm

<%@ LANGUAGE="VBSCRIPT" %>

<html>
<head>

<title>init.asp</title>

<script type="text/javascript">
<!--
function myfunction()
{
var name, email, tel, rabbid, pwd, DBConn;


name = document.forms[0].elements[0].value;
email = document.forms[0].elements[1].value;
tel = document.forms[0].elements[2].value;
rabbid = document.forms[0].elements[3].value;
pwd = document.forms[0].elements[4].value;
alert("HELLO");

DBConn = Server.CreateObject("ADODB.Connection");
DBConn.open ("Shop");
DBConn.Execute("insert into tblcustomer values (1,'mosa','mosa@xyz.com','00-123456',1,'mosa')");
alert("New customer inserted");
}
//-->
</script>
</head>

<H1> Register a new customer</H1>

<form >
<table border="4">
<tr>
<td>
Namn:</td>
<td> <input type="text" name="Name"> </td>
</tr>

<tr>
<td>
Email:</td>
<td> <input type="text" name="Email"> </td>
</tr>

<tr>
<td>
Telefonnummer:</td>
<td> <input type="text" name="Telefon"> </td>
</tr>

<tr>
<td>
Rabattgroup:</td>
<td>
<select>
<option value ="professional">professional
<option value ="Student">Student
<option value ="unemployed">unemployed
</select>
</td>
</tr>

<tr>
<td>
Password:</td>
<td> <input type="password" name="Password"> </td>
</tr>

</table>
</br>
<!--<input type="submit" onsubmit/>-->
<input type="submit" name="B1" value="Add a customer">
<input type="reset" onreset/>
</form>
</br>
<script type="text/javascript">
<!--
document.forms[0].onsubmit = myfunction;
//-->
</script>
</body>
</html>

adamdelves
01-31-2003, 03:11 AM
Hi I've just read over the script. It appears you have got ASP and JavaScript a bit mixed up.

ASP is a server scripting language. This means that when you request an ASP page it is first parsed through and interpreter and any ASP scripts are executed. Then the page is sent to the clients web browser. The clients web browser does not have an ASP interpreter. This means that ASP cannot popup a dialog box on the clients screen because all the code is executed on the server. It can however access database and other programs the run on the server. These programs can be used to store and manipulate data. E.g. queries can be execute on an SQL database such as mySQL.

JavaScript on the other hand is and event driven client side scripting language JavaScript code is ignored by the web server unless explicitly told otherwise and sent to the clients browser. The clients web browser has a JavaScript interpreter. This means that JavaScript can respond to events on the web page and display dialog boxes on the clients machine. It cannot however execute server side programs such as databases for security reasons.

It appears that in your script you have tried to add ASP code to a client side Javasctipt function. The caused the Javascript interpreter to crash as no Server object exists in JavaScript.

When embedding ASP code into a asp page it must be enclosed in <% %> tags. This tells the server side ASP interpreter that there is an ASP script to execute. This is an example of an ASP page.
<pre>
&lt;%@ LANGUAGE="VBSCRIPT" %&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Hello World&lt;/title&gt;
&lt;body&gt;
&lt;% For x = 1 to 6 %&gt;
&lt;h&lt;% = x %&gt;&gt;Hello World&lt;/h&lt;% = x %&gt;
&lt;% Next %&gt;
&lt;/body&gt;
</pre>
When you view the source in the web browser you’ll see the following:
<pre>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Hello World&lt;/title&gt;
&lt;body&gt;
&lt;h1&gt;Hello World&lt;/h1&gt;
&lt;h2&gt;Hello World&lt;/h2&gt;
&lt;h3&gt;Hello World&lt;/h3&gt;
&lt;h4&gt;Hello World&lt;/h4&gt;
&lt;h5&gt;Hello World&lt;/h5&gt;
&lt;h6&gt;Hello World&lt;/h6&gt;
&lt;/body&gt;
</pre>
As you can see the asp script has gone and all that remains is its output. You there fore need to send the form back to the server and open the database with ASP. You can use the client side JavaScript to validate the data and ensure the client has entered information into all the fields. Here is what your first page should look like:
<pre>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;init.asp&lt;/title&gt;
&lt;script language="Javascript"&gt;
function validate (){
if (document.forms[0].elements[0].value == '' || document.forms[0].elements[1].value == '' ||
document.forms[0].elements[2].value == '' || document.forms[0].elements[3].value == '' ||
document.forms[0].elements[4].value == '') {
alert("Some of your Input is missing form cannot be submitted");
return 0;
} else
return 1;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;H1&gt; Register a new customer&lt;/H1&gt;
&lt;form action="insert.asp" method="post" onSubmit="if (! validate()) return false;"&gt;
&lt;table border="4"&gt;
&lt;tr&gt;
&lt;td&gt; Namn:&lt;/td&gt;
&lt;td&gt; &lt;input type="text" name="Name"&gt; &lt;/td&gt;
&lt;/tr&gt; &lt;tr&gt;
&lt;td&gt;Email:&lt;/td&gt;
&lt;td&gt; &lt;input type="text" name="Email"&gt; &lt;/td&gt;
&lt;/tr&gt; &lt;tr&gt;
&lt;td&gt;Telefonnummer:&lt;/td&gt;
&lt;td&gt; &lt;input type="text" name="Telefon"&gt; &lt;/td&gt;
&lt;/tr&gt; &lt;tr&gt;
&lt;td&gt;Rabattgroup:&lt;/td&gt;
&lt;td&gt; &lt;selectname=”rebbid”&gt;
&lt;option value ="professional"&gt;professional
&lt;option value ="Student"&gt;Student
&lt;option value ="unemployed"&gt;unemployed
&lt;/select&gt;
&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;Password:&lt;/td&gt;
&lt;td&gt; &lt;input type="password" name="Password"&gt; &lt;/td&gt;
&lt;/ tr&gt;
&lt;/table&gt;

&lt;input type="submit" value="Add a customer"&gt;
&lt;input type="reset"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
When the form is submitted the data entered into is passed to the server and then o the page insert.asp this page can then contain an ASP script which uses the request objects QueryString method to access these data using the names defined in the html document above and enter them into the database:
<pre>
&lt;%
name = Request.Form(“Name”)
email = Request.Form(“Email”)
tel = Request.Form(“Telefon”)
rabbid = Request.Form(“rebbid”)
pwd = Request.Form(“Password”)

DBConn = Server.CreateObject("ADODB.Connection")
DBConn.open ("Shop")
DBConn.Execute("insert into tblcustomer values (1,'mosa','mosa@xyz.com','00-123456',1,'mosa')")
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;New Customer created&lt;/h1&gt;
&lt;/body&gt;
</pre>
Here you have used both JavaScript and ASP to create a form. Hope this helps.

ADAM