最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alert box shows form data when clicking button - Stack Overflow

programmeradmin3浏览0评论

I am looking to create a button at the bottom of a form that will create an alert box that will show the form data entered. Form includes: First Name Last Name Address 1 Address 2 City State Zip Phone Fax

Once the form is pleted, the button is clicked and an alert box pops up showing the form data entered.

Does anyone know how to acplish without the form actually being submitted or validated? There is no database for the form data to be submitted to, so there is no database to pull the information from.

Any help would be greatly appreciated.

I have not included the form code due to its length, but the current code I am working with for the Alert Box looks like this:

 <script>
 function display_alert()
 {
 alert("");
 }
 </script>

 <body>
 <input type="button" onclick="display_alert()" value="Display alert box">
 </body>

I am looking to create a button at the bottom of a form that will create an alert box that will show the form data entered. Form includes: First Name Last Name Address 1 Address 2 City State Zip Phone Fax

Once the form is pleted, the button is clicked and an alert box pops up showing the form data entered.

Does anyone know how to acplish without the form actually being submitted or validated? There is no database for the form data to be submitted to, so there is no database to pull the information from.

Any help would be greatly appreciated.

I have not included the form code due to its length, but the current code I am working with for the Alert Box looks like this:

 <script>
 function display_alert()
 {
 alert("");
 }
 </script>

 <body>
 <input type="button" onclick="display_alert()" value="Display alert box">
 </body>
Share Improve this question edited Jul 15, 2013 at 6:38 4LeafFather asked Jul 15, 2013 at 6:31 4LeafFather4LeafFather 231 gold badge1 silver badge6 bronze badges 3
  • Are you using any java script library like jQuery? – Lekhnath Commented Jul 15, 2013 at 6:36
  • If the form shouldnt get submitted, then you need to return false , from your onclick event of your submit button. as far as displaying the form data in a modal dialog is concerned, you can do it programitically only. i dont think so you will find any difficult in doing the this . – dreamweiver Commented Jul 15, 2013 at 6:37
  • @Lekhnath: I beleive you should pay attention to the tags and understand the question :) – dreamweiver Commented Jul 15, 2013 at 6:38
Add a ment  | 

5 Answers 5

Reset to default 5

If I get it right you need something like this:

<html>
<head>
<script type="text/javascript">
window.onload = function(){
   document.getElementById('send').onclick = function(e){
       alert(document.getElementById("name").value);
       return false;
   }
}
</script>
</head>
<body>
<form method="post">
<input type="text" name="name" id="name" />
<input type="submit" name="send" id="send" value="send" />
</form>
</body>
</html>

I don't really get what you mean with a database to pull the information from, but the example uses a click event to get the data from the form field and shows it in an alert without a submit.

html code:

<html>
<SCRIPT SRC="PR8_4.JS"></SCRIPT>
<body>
<form name=details>
<table>
 <tr><td>ENTER  FRIST NAME:<input type=text  name=fname></td></tr>
 <tr><td>ENTER  LAST  NAME:<input type=text  name=lname></td></tr>
 <tr><td>ENTER  PHONE NUM :<input type=text  name=phnum></td></tr>
</table>
<input type="button" value="Click Me" onclick="display();">
 </form>
</body>
</html>

javascript code:

function display()
{
var x=document.details.fname.value;
var y=document.details.lname.value;
var z=document.details.phnum.value;
alert("FIRST NAME:"+x+"     "+"LAST NAME:"+y+"     "+"PHONE NUMBER:"+z);
}

To stop a form submitting you can create an onsubmit event within the tag and return false - e.g. ...form elements.... This has the benefit of working when someone submits the form by pressing the enter key as well as pressing the submit button.

Thus, to achieve what you desire you could create a function (lets call it formAlert) and call it from the onsubmit event e.g. ...form elements...

The formAlert function would look something like:

function formAlert() {
    alert_string = '';
    alert_string = alert_string + document.getElementById('first_name').value;
    alert_string = alert_string + ' ';
    alert_string = alert_string + document.getElementById('last_name').value;

    alert(alert_string);
}

and this would correspond to a form looking like:

<form id="foo" onsubmit="formAlert(); return false;">
    <p><label for="first_name">First Name<label><input type="text" id="first_name" value="fred" /></p>
    <p><label for="last_name">Last Name<label><input type="text" id="last_name" value="blogs" /></p>

    <p><input type="submit" value="click me" /></p>
</form>

Note1, this won't be a pretty modal box - it'll simply display "fred blogs" in a Javascript alert box.

Note2, if there is a Javascript error your form will still submit (although in the example here it'll submit to itself).

Here is a JS Fiddle demonstrating the above code: http://jsfiddle/D59su/

I think this might be what you're looking for:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="javascriptform.css">
</head>
<body>
    <form name= "details"><div class="box1"><div id="a"><input type="text" name="lastname" placeholder="LAST NAME"></div><br>
                      <div id="b"><input type="text" name="firstname" placeholder="FIRST NAME"></div><br>
                      <div id="c"><input type="e-mail" name="email" placeholder="E-MAIL"></div><br>
                      <div id="d"><input type="password" name="password" placeholder="PASSWORD"></div><br>
                      <div id="sub-button"><button onclick="getdetails();">submit</button></div></form>

    </div>

    <script>
        function getdetails()
        {
var a = document.forms["details"]["lastname"].value;
var b = document.forms["details"]["firstname"].value;
var c= document.forms["details"]["email"].value;

alert("Your name is "+a+" "+b+". Your e-mail is "+c);

        }
    </script>
</body>
</html>

It Is Very Simple

Using .value will help.

HTML:

<form onsubmit="return myFunction()> <input type="text" id="name> <input type="submit" value="SEND">

Use return before your function

Javascript:

function myFunction () {var name = document.getElementById("name").value; alert("Hi " + name)}

After Submitting It Will Show As (If I Write Alex and Submit It)

Hi Alex

Hope it will work

发布评论

评论列表(0)

  1. 暂无评论