PHP constantly regenerates division by zero at line that I've defined my MySQL connection info. the code uses AJAX to change a variable, so the page can regenerate a new string from database.

Here's the code:


<script type="text/javascript">
function newtrl()
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("tr").innerHTML=xmlhttp.responseText;//the input button's ID, is innerHTML the right element for this?
    }
  }
xmlhttp.open("GET","http://www.1barg.com/page.php?trl=",true);
xmlhttp.send();

</script>
</head>

<body>



<?php
$con = mysql_connect(Localhost, bargcom/@localhost, wWI9sNXjUv);//the error line
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db(kusabax22, $con);

$tbl = "CREATE TABLE table (column varchar(255),)";

mysql_query($tbl,$con);

mysql_query("INSERT INTO table (column)
VALUES ('//some random strings)");



$trl=$_GET["trl"];

$rand = mysql_query("UPDATE `troll2` SET col = MD5(RAND()) WHERE condition=TRUE;");
$rand3 = mysql_query("UPDATE `troll2` SET col = MD5(RAND()) WHERE condition=TRUE;");// these are same to avoid T_IF error.
if  ($trl==1)
{ 
 echo "$rand";
} // if user clicked the button below, generate a random string.
else
{
 echo "$rand3";
}// for when page was opened.

mysql_close($con);
?> 

<form id="tr" action="page.php" method="get">
<input type="button" value="New Troll" onclick="newtrl()"/>
</form>// the button. I really don't know what to do here, so I set it to trigger the Ajax function, which I know is not the right thing to do. 
</body>

Thanks in advance.

    you should be using quotes around your connection credentials.

      dagon;10956089 wrote:

      you should be using quotes around your connection credentials.

      Thanks I thought it counts as syntax error.

      I have problems with code, can you help me make it work? It doesn't show the strings that I've entered into the field, can someone take a look at the AJAX part? thanks.

        Aurlito;10956091 wrote:

        Thanks I thought it counts as syntax error.

        the / in your credentials is a divisor so php is trying to divide

        bargcom by @

        which it cant do, hence the error

          Hi, Aurlito
          Like dagon say, you should be add quotes around your connection parameters because with this syntax you induce PHP preprocessor is try to divide bargcom by @ and this is inregular erroneous.

          About Ajax script.
          You don't have open bracket after javascript function

          function newtrl() 

          , this must be

          function newtrl() { ... function body ... }

          In Ajax redirect URL "http://www.1barg.com/page.php?trl=", but it's always will be empty, because you don't catch any value of element, which it to assume. &#1058;herefore you always go to ELSE case in your conditions:

          if  ($trl==1)
          {
              echo "$rand";
          } // if user clicked the button below, generate a random string.
          else
          {
               echo "$rand3";
          }// for when page was opened.
          

          So, to catch some element value and to put in Ajax url request you can do somthing like this :

          ......
          $btnVal = document.getElementById("btn").value;
          .....
          
          HTML ....
          <input type="button" value="New Troll" [B]id="btn"[/B] onclick="newtrl()"/> 
          
          

          So, when you get the values you can add it in some condition

           
          alert(document.getElementById("btn").value);
          
          if( document.getElementById("btn").value == 'New Troll') {
               xmlhttp.open("GET","http://localhost?trl=1",true);
          }else {
               xmlhttp.open("GET","http://localhost?trl=0",true);
          }
          

          or put directly in some URL parameter

          ......
          var btnVal = document.getElementById("btn").value;
          .....
          alert(btnVal );
          
          xmlhttp.open("GET","http://localhost?trl="+btnVal ,true);
          

          I hope this helping you to figure out how to get and assume parameters and values in Ajax request.

            Thanks for help. but I have another problem.

            if( document.getElementById("btn").value == 'New Troll') {
            xmlhttp.open("GET","http://localhost?trl=1",true);
            }else {
            xmlhttp.open("GET","http://localhost?trl=0",true);
            }

            I don't get it, the button's value is always "New Troll" so logically, this condition is always true, hence other Ajax form will never send "0" to the server.

              Aurlito;10956153 wrote:

              Thanks for help. but I have another problem.

              I don't get it, the button's value is always "New Troll" so logically, this condition is always true, hence other Ajax form will never send "0" to the server.

              Yes, of course it is always true. I just try to show you how to get value of some HTML element.🙂
              I'm a little confused and don't understand what exactly you want to happen when click the button.
              Can you explain more clearly what you want to make?

                Saviola;10956157 wrote:

                Yes, of course it is always true. I just try to show you how to get value of some HTML element.🙂
                I'm a little confused and don't understand what exactly you want to happen when click the button.
                Can you explain more clearly what you want to make?

                When user opens the page a random text from database (the table that I create in the code) is generated and displayed in the field. when he clicks the "New Troll" button another text from database is generated and replaced with the initial text.

                I was going to add a contribution to database too but I failed at the first step.

                Ps: any good books to learn PHP and Ajax?

                  If you want to simulate different situation just for example :

                  function newtrl() {
                    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                      xmlhttp=new XMLHttpRequest();
                    } else {// code for IE6, IE5
                      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                  
                    var trl = document.getElementById("trlParameter").value;
                  
                  
                    xmlhttp.onreadystatechange=function(){
                      if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                             document.getElementById("tr").innerHTML=xmlhttp.responseText;//the input button's ID, is innerHTML the right element for this?
                        }
                    }
                    if(  trl == 1) {
                  
                   xmlhttp.open("GET","http://www.1barg.com/page.php?trl=1",true);
                    }else {
                       xmlhttp.open("GET","http://www.1barg.com/page.php?trl=0",true);
                    }
                    xmlhttp.send();
                  }
                  
                  
                  ....................
                  if  ($trl==1)
                  {
                  echo "rand - $rand";
                  } // if user clicked the button below, generate a random string.
                  else
                  {
                  echo "rand3 - $rand3";
                  }// for when page was opened.
                  .........................
                  
                  <form action="" method="get" name="tr" id="tr">
                  <input type="button" value="New Troll" id="btn" onclick="newtrl()"/>
                  <input type="text" name="trlParameter" id="trlParameter" value="<?php echo !($_GET['trl'])?>" disabled="disable">
                  </form>
                  

                  NOTE: about echo "$rand"; - For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

                  http://www.ajaximpact.com/tutorials.php
                  http://www.ajaxtutorial.net/index.php/category/ajax-basics/
                  http://www.xul.fr/en-xml-ajax.html
                  http://www.learn-ajax-tutorial.com/
                  http://viralpatel.net/blogs/2009/04/jquery-ajax-tutorial-example-ajax-jquery-development.html

                    Write a Reply...