How do I combine body onload and button onclick code in same code?

Discussion in 'HTML & Website Design' started by kinjal_sshah, Dec 5, 2012.

  1. #1
    I have wriiten the following code:
    <!--
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Calling Function</title>
    <script>

    function showCustomer(str)
    {
    var xmlhttp;
    if (str=="")
    {
    document.getElementById("txtHint").innerHTML="";
    return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","http://ps8471.persistent.co.in:8080/AjaxCode/DBUpdator?id="+str,true);

    xmlhttp.send();
    }

    function showCustomer1(str,str1)
    {
    var xmlhttp;
    if (str=="")
    {
    //alert(document.getElementById("txtHint").innerHTML);
    document.getElementById("txtHint").innerHTML="";
    return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    // alert(xmlhttp.responseText);
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","http://ps8471.persistent.co.in:8080/AjaxCode/DBUpdatorShow?tag="+str+"&id="+str1,true);
    xmlhttp.send();
    // clears text value
    document.getElementById('tag').value = "";

    }

    function getParams() {
    var idx = document.URL.indexOf('=');
    var tempParams = new Object();
    if (idx != -1) {
    var pairs = document.URL.substring(idx+1, document.URL.length);
    }
    return pairs;
    }
    var p = getParams();

    </script>
    </head>

    <body onload="showCustomer(p)" > <!--onload="showCustomer('I00012353');"-->
    <div id="txtHint">Indent info will be listed here...</div>
    Kinjal's Part
    Intent ID: I00012353
    <br/>
    Tag: Tags from database will come here
    <br/>
    <input type="text" name="tag" id="tag" >

    <button type="submit" onclick="showCustomer1(document.getElementById('tag').value,'I00012353')">Add Tag</button>

    <br></body>
    </html>
    -->


    I want body onload to be displayed and onclick value in text box to be taken as input.but here at the same time both things are happening.please help me out urgently
     
    kinjal_sshah, Dec 5, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Change your server code so that the <body> statement is written to show the customer number you get from the database. Something like

    <body onload="showCustomer(<?php echo $customerNumber; ?> )" > <!--onload="showCustomer('I00012353');"-->
     
    Rukbat, Dec 5, 2012 IP
  3. kinjal_sshah

    kinjal_sshah Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    through show customer funstion I have given call to java code, where that database is linked..some other solution?
     
    kinjal_sshah, Dec 5, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    You're writing a bunch of spaghetti that, I think, would be a lot simpler if you first thought about what you have to do, not how to do it. 'How' comes after you simplify 'what'. (You write computer programs in English before you start to write any code.)

    There are a few problems. If there's no argument to the URL, getParams returns an undefined variable. (You only define 'pairs' if there's an argument, but you return it even if there isn't.) It also defines tempParams but never uses it.

    I00012353 seems to be a constant, so there's no need to send it all over the place.

    And define a single function to create your xmlhttp, then just call it from where you need it. There's no reason to duplicate code, it just slows the load time.
     
    Rukbat, Dec 5, 2012 IP
  5. kinjal_sshah

    kinjal_sshah Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanx Rukbat..I tried single finction..its working now :)
     
    kinjal_sshah, Dec 5, 2012 IP
  6. BarbaraMibram

    BarbaraMibram Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    Thank you so much... I also have the same problem. And you solve my problem. It works...
     
    BarbaraMibram, Dec 5, 2012 IP