Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-09-2012, 02:45 AM   PM User | #1
Red Dragon
New to the CF scene

 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Red Dragon is an unknown quantity at this point
trouble with addEventListener

I'm trying to gets this code to work so that when you click submit it will send whats in the textbox to the php script but its not sending the post request.
Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                            <script type="text/javascript" src="test2.js"></script>
                            <link rel="stylesheet" type="text/css" href="test4.css" id="linkPage" />
                            <title>
                                    test site
                            </title>
           
                    </head>
                    <body>
            <div id="yourdivname">
            </div>
            <form action="" method="post">
                    <input type="text" id="mytextbox" />
                    <input type="submit" id="submit1" value="Send" />
            </form>
            <SCRIPT>
        var submit1 = document.getElementById('submit1');
                submit1.addEventListener("submit",function sendmessage (e) {
                var message = form.mytextbox.value;
                xhr.open("POST", "send.php");
                xhr.send(JSON.stringify(message));
                e.preventDefault()
                    },false);
            </SCRIPT>
     
    </html>

Last edited by Red Dragon; 09-09-2012 at 05:25 AM..
Red Dragon is offline   Reply With Quote
Old 09-09-2012, 06:05 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,389
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Try working with this:
Code:
<form action="" method="post" id="myform">  Add this ID
        <input type="text" id="mytextbox" />
        <input type="submit" id="submit1" value="Send" />
</form>


<script type="text/javascript">
var submit1 = document.getElementById('myform');  Change this !!!
sunfighter is online now   Reply With Quote
Old 09-11-2012, 01:04 AM   PM User | #3
Red Dragon
New to the CF scene

 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Red Dragon is an unknown quantity at this point
ok I got it to send the post request but the php script doesn't see the request. the dump from the $_POST is array(0) { }
here is the updated code

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                        <script type="text/javascript" src="test2.js"></script>
                        <link rel="stylesheet" type="text/css" href="test4.css" id="linkPage" />
                        <title>
                                test site
                        </title>
        
                </head>
                <body>
        <div id="yourdivname">
        </div>
        <form action="" method="post" id="myform">
                <input type="text" id="mytextbox" />
                <input type="submit" id="submit1" value="Send" />
        </form>
	<script type="text/javascript">
var submit1 = document.getElementById('myform');
            submit1.addEventListener("submit",function sendmessage (e) {
            var xhr = new XMLHttpRequest
            xhr.open("POST", "send.php");
            xhr.send(new FormData(myform));
	    submit1.reset();
            e.preventDefault()
		},false);
        </SCRIPT>

</html>
Red Dragon is offline   Reply With Quote
Old 09-12-2012, 01:45 PM   PM User | #4
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
a few changes ....
(doesNot work with IE9 and below)

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                        <script type="text/javascript" src="test2.js"></script>
                        <link rel="stylesheet" type="text/css" href="test4.css" id="linkPage" />
                        <title>
                                test site
                        </title>
        
                </head>
                <body>
        <div id="yourdivname">
        </div>
        <form action="" method="post" id="myform">
                <input type="text" name="mytextbox" />
                <input type="submit" id="submit1" value="Send" />
        </form>
	<script type="text/javascript">
var xhr = new XMLHttpRequest
var submit1 = document.getElementById('myform');
            submit1.addEventListener("submit",function sendmessage (e) {            
            xhr.open("POST", "send.php",false);
            xhr.send(new FormData(submit1));
	    submit1.reset();
            e.preventDefault()
		alert(xhr.responseText)
},false);
	
        </SCRIPT>

</html>
send.php
PHP Code:
<?PHP
echo $_POST["mytextbox"]
?>
DaveyErwin is offline   Reply With Quote
Old 09-12-2012, 07:52 PM   PM User | #5
Red Dragon
New to the CF scene

 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Red Dragon is an unknown quantity at this point
I got it to work what one of the problems was that new FormData wasn't working so i use myFormData.append("message", message);

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                        <script type="text/javascript" src="test2.js"></script>
                        <link rel="stylesheet" type="text/css" href="test4.css" id="linkPage" />
                        <title>
                                test site
                        </title>
        
                </head>
                <body>
        <div id="yourdivname">
        </div>
        <form action="" method="post" id="myform">
                <input type="text" id="mytextbox" />
                <input type="submit" id="submit1" value="Send" />
        </form>
	<script type="text/javascript">
var submit1 = document.getElementById('myform');
            submit1.addEventListener("submit",function sendmessage (e) {
            var message = myform.mytextbox.value;
	    var myFormData = new FormData();
	    myFormData.append("message", message);
	    var xhr = new XMLHttpRequest
            xhr.open("POST", "send.php");
            xhr.send(myFormData);
	    submit1.reset();
            e.preventDefault()
		},false);
        </SCRIPT>

</html>
Red Dragon is offline   Reply With Quote
Old 09-12-2012, 07:58 PM   PM User | #6
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
here is why it doesn't work for you
but it does for me
<input type="text" id="mytextbox" />
should be
<input type="text" name="mytextbox" />
DaveyErwin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:48 PM.


Advertisement
Log in to turn off these ads.