javascript - Issues with json_encode -


i want retrieve mail , password , show them json format in <p> element. problem when click on submit, nothing happens.. wrong code?

<html> <head>  </head> <body>  <script> function myfunction() { var mail = document.getelementbyid("mail1").value;         var pass = document.getelementbyid("password1").value;         //document.getelementbyid("ici").innerhtml = mail + " " + pass  ;         tab['mail'] =  mail;         tab['password'] =  password;         var output = json_encode(tab);         document.getelementbyid("ici").innerhtml = output;     }  </script>  mail: <input type="text"  name="mail" id="mail1"> password: <input type="text"  name="password" id="password1"> <button onclick="myfunction()" > submit</button>  <p>ici: <span id="ici"></span></p> </body> </html> 

in javascript, convert json using json.stringify, not json_encode (which php):

var output = json.stringify(tab); 

but code quoted fail because don't define tab anywhere, , you've used password rather pass (the name gave variable). may have meant:

var mail = document.getelementbyid("mail1").value; var pass = document.getelementbyid("password1").value; var output = json.stringify({     mail: mail,     password: pass }); document.getelementbyid("ici").innerhtml = output; 

or more concisely (but not easy debug):

document.getelementbyid("ici").innerhtml = json.stringify({     mail: document.getelementbyid("mail1").value,     password: document.getelementbyid("password1").value }); 

Comments