Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

JavaScript function to insert into database

javascript sql database insert

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts

Posted 20 April 2013 - 03:34 PM

I want custom JavaScript function which will allow me to make insertion into my database.

This is the code I have tried:

<section class="featured">
        <div class="content_wrapper">
            <input type="text" style="height:40px; width: 200px" name="ime"/>
            <input type="text" style="height:40px; width: 200px" name="prezime" />
        </div>
        <div class="button">
            <button type="submit" onclick="Insert()" style="height: 40px; width: 430px">Unesi u bazu</button>
        </div>
    </section>
    <script>
        function Insert() {
            /* Getting access to the database */
            var connection = new ActiveXObject("ADODB.Connection");
            var connectionstring = "";
            connection.Open(connectionstring);

            /* JavaScript obect to access a SQL query's results */
            var rs = new ActiveXObject("ADODB.Recordset");

            /* TODO: Get the last UID */
            var sql = "INSERT INTO Clanovi(Ime, Prezime) VALUES (ime,prezime)";
            
            rs.Open(sql, connection);

            /* Closing the connections */
            //rs.close;
            connection.close;
        }
      </script>

I have let connectionstring empty so now one will try to connect to my server :)

I have try with the code above, but when I clicked the button, nothing happened. I have checked my database and there was no changes. So, I guess that something is wrong with JavaScript part. I am new to this thing and I have copied this code from the internet :)


Microsoft Student Partner, Microsoft Certified Professional


#2 JasonKnight

JasonKnight

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 312 posts

Posted 20 April 2013 - 06:26 PM

I'm pretty sure that functionality is blocked in modern IE for security reasons... You could try lowering the security settings for local activity, but I'd probably disconnect from the Internet first. Though, I have a stupid question, you're only trying to do that in IE, right?
The only thing about Dreamweaver that can be considered professional grade tools are the people promoting it's use.

#3 JasonKnight

JasonKnight

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 312 posts

Posted 20 April 2013 - 07:21 PM

Actually, I don't see where you're extracting the values from your two inputs at the top in your script... That query would in my mind should be throwing an error, though my adodb experience is limited.

Shouldn't it be something more like this?
<form class="dbInsert" onsubmit="dbInsert(); return false;">
	<fieldset>
		<input type="text" id="ime" name="ime" />
		<input type="text" id="prezime" name="prezime" />
	</fieldset>
	<div class="submitAndHiddens">
		<input type="submit" class="submit" value="Unesi u bazu" />
	</div>
</form>

<script type="text/javascript"><!--

function dbInsert() {

	var
		connection = new ActiveXObject("ADODB.Connection"),
		recordSet = new ActiveXObject("ADODB.Recordset"),
		connectionString = '';
		
	connection.Open(connectionString);
	recordSet.Open(
		"INSERT INTO Clanovi(Ime, Prezime) VALUES ('" +
			document.getElementById('ime').value + "','" +
			document.getElementById('prezime').value + 
		"')",
		connection
	);
	
	recordSet.close; 
	connection.close;
	
} // dbInsert

--></script>

Edited by JasonKnight, 21 April 2013 - 02:12 AM.

The only thing about Dreamweaver that can be considered professional grade tools are the people promoting it's use.

#4 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts

Posted 21 April 2013 - 04:22 AM

I'm pretty sure that functionality is blocked in modern IE for security reasons... You could try lowering the security settings for local activity, but I'd probably disconnect from the Internet first. Though, I have a stupid question, you're only trying to do that in IE, right?

 

I am working that on Google Chrome.


Microsoft Student Partner, Microsoft Certified Professional


#5 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts

Posted 21 April 2013 - 04:41 AM

http://i.imgur.com/s9q7Kze.png

 

And as you can see from the picture, there was no connections in the last 6 hours. I have putted connection string, but I still don't get it why there was no connection attempt on my server.


Microsoft Student Partner, Microsoft Certified Professional


#6 JasonKnight

JasonKnight

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 312 posts

Posted 21 April 2013 - 04:50 AM

I am working that on Google Chrome.

Well there's your problem... there is no such thing as ActiveX in anything but IE!!!

You can't do "new ActiveXObject" in Chrome, ActiveX is an IE specific non-standard class. It only exists in jScript, NOT JavaScript or ECMAScript!
The only thing about Dreamweaver that can be considered professional grade tools are the people promoting it's use.

#7 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts

Posted 21 April 2013 - 05:01 AM

Well there's your problem... there is no such thing as ActiveX in anything but IE!!!

You can't do "new ActiveXObject" in Chrome, ActiveX is an IE specific non-standard class. It only exists in jScript, NOT JavaScript or ECMAScript!

 

LOL. And what should I do to make it work on every browser?



And when I test it on IE10, I get next exception:

Unhandled exception at line 60, column 9 in http://localhost:39464/

0x800a0e84 - JavaScript runtime error: Safety settings on this computer prohibit accessing a data source on another domain.

If there is a handler for this exception, the program may be safely continued.

For the next line of code:

connection.Open(connectionString);

Microsoft Student Partner, Microsoft Certified Professional


#8 JasonKnight

JasonKnight

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 312 posts

Posted 21 April 2013 - 05:27 PM

And when I test it on IE10, I get next exception:

Which is EXACTLY what my first post in this thread was talking about... that functionality has been blocked in modern browsers unless both elements are local and accessed on the same domain/IP, becuase it's a GAPING security hole anywhere else. The mere notion of passing the SQL login and password to client-side code is absurdly insecure even in the best of times.
 

LOL. And what should I do to make it work on every browser?

Generally speaking you don't, at least not using client-side scripting. This is why you NEVER see ANYTHING doing this. There is some new javascript called "local storage" that people keep calling HTML 5 even though it ISN'T, that could work in other browsers, but it too is a insecure mess.

There's a reason things like database access is usually handled in SERVER-SIDE languages like PHP, ASP, PERL, PYTHON, RUBY, etc, etc...

You are basically trying to do something from javascript that to be brutally frank, has no business being done in client-side code in the first place since anyone could just do "view source", and have unrestricted access. That's why it's been actively blocked for like a decade or so.

I was a little shocked to see someone even trying to use ADODB via ActiveX in this day and age, but to be fair with 90% of the websites, tutorials and books in the topic of web development still having their heads firmly wedged up 1997's backside... it shouldn't have been that big a surprise.

Lemme put it this way, as my markup shows you should have a perfectly good FORM -- either ajax the submit, or just submit it normally to SERVER SIDE code for processing. Good rule of thumb, you start using INPUT without a FORM around it, you're probably doing something WRONG!

Oh, and when I say client side I mean in a browser -- which means Javascript. ADO is still quite useful in C#, VB and other .NET languages -- but as a ActiveX mechanism in the browser, it's a insecure headache and a half I've really not heard of anyone bothering even TRYING to use in at least a decade. Anything you do in the browser has certain security flaws and a lot of stuff you could do fifteen years ago is just outright not allowed; and there's a LOT of stuff you can do in jScript (IE) that you can't do Rest of World. RoW these things just don't even exist, for good reason! DirectX filters, ActiveX objects, behavior scripts (.htc), VML -- all proprietary IE stuff that isn't part of the W3C public specifications -- and again, for good reason. Insecure, unreliable, bloated, or just plain bad thinking that is why amongst experts IE over the past ten years has been the security equivalent of stripping naked, painting yourself in glow-in-the dark paint and running into the woods of North Vietnam in 1968 waving a spotlight screaming "shoot me, shoot me"

Edited by JasonKnight, 21 April 2013 - 05:27 PM.

The only thing about Dreamweaver that can be considered professional grade tools are the people promoting it's use.

#9 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts

Posted 21 April 2013 - 05:34 PM

Ok. I have learned something new. But you didn't tell me what my JavaScript should look like if I want to make it work on every popular web browser (IE, Chrome, Firefox). I am here to learn a new things since I have just started with JavaScript.


Microsoft Student Partner, Microsoft Certified Professional


#10 JasonKnight

JasonKnight

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 312 posts

Posted 22 April 2013 - 05:44 AM

You're missing what I'm saying, there IS NO JAVASCRIPT to do what you are trying to do cross-browser. DOES NOT EXIST! For all the reasons I outlined.

The closest equivalent in other browsers is "web storage", and support for that is nonexistant and I fully expect it to be blocked or unsupported in the near future; since like much of the stuff shoved under HTML 5's banner it's a security nightmare.

Hence why I listed all those other languages since you should NOT be trying to do this client side (javascript) as this is SERVER-SIDE's job; PHP, ASP, PERL, PYTHON, RUBY, .NET -- pick one, I suggest PHP if you're just starting since it's VERY well documented and you're more likely to get meaningful help on forums like these.

Edited by JasonKnight, 22 April 2013 - 05:47 AM.

The only thing about Dreamweaver that can be considered professional grade tools are the people promoting it's use.

#11 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 22 April 2013 - 08:01 AM

What you SHOULD do is use JavaScript to make an AJAX call to the server. The server will then use server-side scripting (PHP, ASP.NET, etc) to connect to the database, perform logic, and send back an appropriate response. Once that is done, your JavaScript will process the AJAX response to update the page.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download