Jump to content

Beginner Question Ajax


rugzo

Recommended Posts

Hi All,

 

i want to use ajax in my php page. Here is what i have* --->

 

<script language="javascript" type="text/javascript">

      <!--

      // Get the HTTP Object

      function getHTTPObject(){

      if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");

      else if (window.XMLHttpRequest) return new XMLHttpRequest();

      else {

      alert("Your browser does not support AJAX.");

      return null;

      }

      }



      // Change the value of the outputText field

      function setOutput(){

      if(httpObject.readyState == 4){

      document.getElementById('outputText').value = httpObject.responseText;

      }



      }



      // Implement business logic

      function doWork(){

      httpObject = getHTTPObject();

      if (httpObject != null) {

      httpObject.open("GET", "upperCase.php?inputText="

      +document.getElementById('inputText').value, true);

      httpObject.send(null);

      httpObject.onreadystatechange = setOutput;

      }

      }



      var httpObject = null;



      //-->

      </script>



      <form name="testForm">

      Input text: <input type="text" onkeyup="doWork();" name="inputText" id="inputText" /> <br>

      Output text: <input type="text" name="outputText" id="outputText" />

      </form>

 

This sends the input to my other php page and returns the calculated data back. But my problem is i have multiple fields in a form which is also created dynamicallly. How can i get the datas from multiple fields and not only one specific.

 

 

Thanks...

 

 

 

 

Link to comment
Share on other sites

I have forgotten to mention that i have one form with two seperate inputs. So i cannot simply get all the inputs. Its something like half of the inputs have ids with even and the other half with odd numbers and i have to get them into two diffrent codes like above and send them that way.

Link to comment
Share on other sites

I recommend doing this with Dojo.

http://www.dojotoolkit.org/

 

Documentation:

http://api.dojotoolkit.org/

 

Specific to your problem:

http://api.dojotoolkit.org/jsdoc/1.3/dojo.xhrPost

http://api.dojotoolkit.org/jsdoc/1.3/dojo.query

http://api.dojotoolkit.org/jsdoc/1.3/dojo.formToObject

http://api.dojotoolkit.org/jsdoc/1.3/dojo.formToJson

 

Link to comment
Share on other sites

How can i get the datas from multiple fields and not only one specific.

 

Can you show the code that generates these multiple fields?

Also I agree with roopur18 that using a js framework for this would recommended. Just not necessarily dojo.

 

Dojo, jQuery, mootools, prototype YUI are all fine frameworks.

Link to comment
Share on other sites

Dojo is the only one that I'm familiar with and I know that what he's asking for is a breeze with it, which is why I recommended it.

 

And I only recommend a framework because I don't feel like wading through yet another "I-tried-to-make-my-own-XHR-calls-and-it-doesn't-work" thread.  These things are a dime a dozen.  :(

Link to comment
Share on other sites

Sorry i didn't exactly understand what you mean with code tags but i will put in the whole php part. But basically this is a setup interface. On the top you select forms which you also build at that time dinamically. You have a main category. Each main category is a form. If you build a main category you will also build the whole form. You have to build the subcategories which also have items. All of them are inputs. the subcategories connected to the same main categories have to have a total point of 100, and the items connected to the same sub categories have to have the also 100 points together. I will copy paste the middle part of my code which builds the form. If its not enough i can paste the whole code.

 

 

<?php

 

$names = $_GET['cat'].$catname1;

 

//form input ids

 

$mainid = "mainid00" ;

$mainname = "mainname00" ;

$subname = "subname00" ;

$itemname = "itemname00" ;

$subid = "subid00" ;

$itemid = "itemid00" ;

$sublp = "subloc00" ;

$subgp = "subgl00" ;

$itemlp = "itemlp00" ;

$itemglp = "itemglp00" ;

$itemlnc = "itemlnc00" ;

$itemgnc = "itemgnc00" ;

$gzero = "gzero00" ;

$lzero = "lzero00" ;

$glac = "glac00" ;

$locac = "locac00" ;

$descr = "descr00" ;

$mdelet = "mdelet00" ;

$sdelet = "sdelet00" ;

$idelet = "idelet00" ;

$sturn = 0 ;

$iturn = 0 ;

 

//ana kategori

$sqlm = mysql_query("select * from qasetup where cattype='main' and catname='$names'");

 

echo "<table border='1'>

      <tr>

      <tr><td colspan='1'><font size='5'><b><center>Name Setup</center></b></font></td>

      <td colspan='6'><font size='5'><b><center>Point Setup</center></b></font></td>

      <td colspan='5'><font size='5'><b><center>Main Setup</center></b></font></td></tr>" ;

 

while($m = mysql_fetch_array($sqlm)){

 

    $mcat = $m[catname] ;

    $mtype = $m[cattype] ;

 

 

    echo "<tr><td><input type='hidden' name='".$mainid."' value='".$m[id]."'><input type='text'

    name='".$mainname."' value='".$mcat."'></td><td></td><td></td><td></td>

    <td></td><td></td><td></td><td></td><td></td><td></td></td>

    <td></td>

    <td>delete<input type='checkbox' name='".$mdelet."'></td></tr><br><br>";

 

    $mname = $_POST[$mainname] ;

 

    if($_POST[$mdelet]){

 

      $idmm = $_POST[$mainid] ;

 

 

        $sqlsdelete = mysql_query("delete from qasetup where id = '$idmm'") or die (mysql_error());

        $sqlsdelet = mysql_query("delete from qasetup where parent = '$mcat'") or die (mysql_error());

 

      }

 

 

//subkategori

 

      $sqls = mysql_query("select * from qasetup where parent = '$mcat' and cattype = 'sub'");

        while($s = mysql_fetch_array($sqls)){

 

    //check global local

      $sloc = $s[loc];

      $sglob = $s[glob];

 

      if($sloc == 0){

        $lsusage = 'disabled';

      }else{$lsusage = 'enabled';}

      if($sglob == 0){

        $gsusage= 'disabled';

      }else{$gsusage = 'enabled';}

 

          $scat = $s[catname];

          echo "<tr><td><input type='hidden' name='".$subid."' value='".$s[id]."'>     

          <input type='text' name='".$subname."' value='".$scat."'></td>

          <td>    <font size='2'> LP :<input type='text' name=".$sublp." size='2' value='".$s[lp]."' ".$lsusage."></td>

          <td>  GP :<input type='text' name=".$subgp." size='2' value='".$s[gp]."' ".$gsusage.">

          </font></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>

          <td>delete<input type='checkbox' name='".$sdelet."'<br><br></td></tr>";

 

$sname = $_POST[$subname] ;

 

  if($_POST[$sdelet]){

 

      $ids = $_POST[$subid] ;

      $sname = $_POST[$subname] ;

 

        $sqlsdelete = mysql_query("delete from qasetup where id = '$ids'") or die (mysql_error());

        $sqlsdelet = mysql_query("delete from qasetup where subparent = '$sname' and

parent = '$mcat' and cattype ='item'") or die (mysql_error());

 

      }

 

 

      //check

 

 

  if($sname != $s[catname] and $s[cattype] == 'sub' and $s[parent] == $mname){

 

  $sqlsubnamecheck = mysql_query("select * from qasetup where cattype = 'sub'

  and parent = '$mname'");

 

    while($snamecheck = mysql_fetch_array($sqlsubnamecheck)){

      if($sname == $snamecheck[catname]){

 

 

      $sname = $s[catname] ;

      echo "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=http://localhost/xraytest/qa/qasetup.php?cat=$mname&cmd1=error1'>";

        }

            }

                }

 

 

        //endcheck

 

 

if($_POST['gonder']){

$sqle = mysql_query("update qasetup set subparent = '$sname'

where subparent = '$scat' and parent = '$mcat' and cattype ='item'") or die (mysql_error());

 

$ids = $_POST[$subid] ;

 

 

$slp = $_POST[$sublp] ;

$sgp = $_POST[$subgp] ;

 

$sql = mysql_query("update qasetup set lp = '$slp', gp = '$sgp', catname = '$sname'

where id = '$ids'") or die (mysql_error());

  }

          $sdelet++ ;

          $subname++ ;

          $subid++ ;

          $sublp++ ;

          $subgp++ ;

          $sturn++ ;

 

 

 

 

//item

 

          $sqlimis = mysql_query("select * from qasetup where parent = '$mcat' and subparent = '$scat' and cattype = 'item'");

    while($it = mysql_fetch_array($sqlimis)){

 

      //check global local

      $itl = $it[loc];

      $itg = $it[glob];

 

      if($itl == 0){$lusage = 'disabled';}else{$lusage = 'enabled';}

      if($itg == 0){$gusage = 'disabled';}else{$gusage = 'enabled';}

 

      if($itl == 1){$locacpa = 'checked';}else{}

      if($itg == 1){$glacpa = 'checked';}else{}

 

      if($it[lzero]==1){$llzero = 'checked';}else{$llzero = '';}

      if($it[gzero]==1){$ggzero = 'checked';}else{$ggzero = '';}

 

          $icat = $it[catname];

  $comment = $it[description];

          echo "<tr><td><input type='hidden' name='".$itemid."' value=".$it[id]."><a onMouseOver=doalt('".$comment."')

          onMouseOut=realt()>     

                   <input type='text' name='".$itemname."' value='".$icat."'</a></td>

          <td>    <font size='2'>

          LP :<input type='text' name='".$itemlp."' size='1' value='".$it[lp]."' ".$lusage."></td><td>

            LNC :<input type='text' name='".$itemlnc."' size='1' value='".$it[lnc]."' ".$lusage."></td><td>

            LZero :<input type='checkbox' name='".$lzero."' ".$llzero."  ".$lusage."></td><td>  

             GP :<input type='text' name='".$itemglp."' size='1' value='".$it[gp]."' ".$gusage."></td><td>

             GNC :<input type='text' name='".$itemgnc."' size='1' value='".$it[gnc]."' ".$gusage."></td><td>

            GZero :<input type='checkbox' name='".$gzero."' ".$ggzero." ".$gusage."></font><br></td>

          <td>Description:<input type='text' name='".$descr."' value='".$comment."'></td><td></td>

          <td>local<input type='checkbox' name='".$locac."' ".$locacpa.">

                  global<input type='checkbox' name='".$glac."' ".$glacpa."></td>

          <td></td>

          <td>delete<input type='checkbox' name='".$idelet."'><br><br></td></tr>";

 

 

      if($_POST[$idelet]){

 

      $idi = $_POST[$itemid] ;

 

        $sqlidelete = mysql_query("delete from qasetup where id = '$idi'") or die (mysql_error());

 

      }

 

          if($_POST['gonder']){

$itemna = $_POST[$itemname] ;

$idi = $_POST[$itemid] ;

$itlp = $_POST[$itemlp] ;

$itgp = $_POST[$itemglp] ;

$itlnc = $_POST[$itemlnc] ;

$itgnc = $_POST[$itemgnc] ;

$desc = $_POST[$descr] ;

 

if($_POST[$gzero]){$itgz=1;}else{$itgz=0;}

if($_POST[$lzero]){$itlz=1;}else{$itlz=0;}

if($_POST[$glac]){$gac=1;}else{$gac=0;}

if($_POST[$locac]){$lac=1;}else{$lac=0;}

 

//check

 

 

  if($itemna != $it[catname] and $it[cattype] == 'item' and $it[parent] == $mname and $it[subparent] == $sname){

 

  $sqlitemnamecheck = mysql_query("select * from qasetup where cattype = 'item'

  and parent = '$mname' and subparent = '$sname'");

 

    while($inamecheck = mysql_fetch_array($sqlitemnamecheck)){

      if($itemna == $inamecheck[catname]){

 

 

      $itemna = $it[catname] ;

      echo "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=http://localhost/xraytest/qa/qasetup.php?cat=$mname&cmd=error'>";

        }

            }

                }

 

 

        //endcheck

 

$sqli = mysql_query("update qasetup set lp = '$itlp', gp = '$itgp', lnc = '$itlnc', gnc = '$itgnc',

lzero ='$itlz', gzero = '$itgz', loc = '$lac', glob = '$gac', description = '$desc', catname = '$itemna'

where id = '$idi'") or die (mysql_error());

 

        }

          $idelet++ ;

          $itemname++ ;

          $itemid++ ;

          $itemlp++ ;

          $itemglp++ ;

          $itemlnc++ ;

          $itemgnc++ ;

          $gzero++ ;

          $lzero++ ;

          $itemlnc++ ;

          $itemgnc++ ;

          $glac++ ;

          $locac++ ;

          $descr++ ;

          $iturn++ ;

 

 

    }

        }

}

 

 

 

 

 

?>

Link to comment
Share on other sites

Dojo is the only one that I'm familiar with and I know that what he's asking for is a breeze with it, which is why I recommended it.

 

And I only recommend a framework because I don't feel like wading through yet another "I-tried-to-make-my-own-XHR-calls-and-it-doesn't-work" thread.  These things are a dime a dozen.  :(

Yeah I hear you on that. Also manipulating the DOM with just vanilla javascript is just a whole lot less fun in comparison with using  a JS framework for that task.

 

Sorry i didn't exactly understand what you mean with code tags but i will put in the whole php part.

He meant wrap your code between [ code ] [ /code ] (without the spaces). It will highlight php code and put it inside a codebox.

 

But basically this is a setup interface. On the top you select forms which you also build at that time dinamically. You have a main category. Each main category is a form. If you build a main category you will also build the whole form. You have to build the subcategories which also have items. All of them are inputs. the subcategories connected to the same main categories have to have a total point of 100, and the items connected to the same sub categories have to have the also 100 points together. I will copy paste the middle part of my code which builds the form. If its not enough i can paste the whole code.

followed by load of php and html....

 

Whoaah!!! hang on right there buddy you lost me there.

I thought you meant dynamically add form fields using javascript but you meant adding fields with PHP I think.  Could you post a sample of the HTML that is generated by the php with the multiple forms? Maybe that way a part of your interface is visible which makes it more clear what you are trying to accomplish.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.