php - Ajax generated form which also has a ajax call -


hello stackoverflow!,

ok question might long, first of im going write want accomplish.

i have dropdown menu 'regions' (of country), when region selected there function onchange gets value , goes ajax function, php file echo result (which clients in region), , results echo form each client, form delete , edit button.

when click on edit, ajax call function too, echo form can edit registry.

script in index.php

<script type="text/javascript">         function showuser(str){             if (str=="")               {               document.getelementbyid("mostrarclientes").innerhtml="";               return;               }              if (window.xmlhttprequest)               {// code ie7+, firefox, chrome, opera, safari               xmlhttp=new xmlhttprequest();               }             else               {// code ie6, ie5               xmlhttp=new activexobject("microsoft.xmlhttp");               }             xmlhttp.onreadystatechange=function()               {               if (xmlhttp.readystate==4 && xmlhttp.status==200)                 {                 document.getelementbyid("mostrarclientes").innerhtml=xmlhttp.responsetext;                 }               }             document.getelementbyid("mostrarclientes").innerhtml='espere un momento porfavor...';               xmlhttp.open("get","verclientesajax.php?ver="+str,true);             xmlhttp.send();         }          function edituser(str){             if (str=="")               {               document.getelementbyid("editarclientes").innerhtml="";               return;               }              if (window.xmlhttprequest)               {// code ie7+, firefox, chrome, opera, safari               xmlhttp=new xmlhttprequest();               }             else               {// code ie6, ie5               xmlhttp=new activexobject("microsoft.xmlhttp");               }             xmlhttp.onreadystatechange=function()               {               if (xmlhttp.readystate==4 && xmlhttp.status==200)                 {                 document.getelementbyid("editarclientes").innerhtml=xmlhttp.responsetext;                 }               }             document.getelementbyid("editarclientes").innerhtml='espere un momento porfavor...';               xmlhttp.open("get","editarclientesajax.php?edit="+str,true);             xmlhttp.send();         } </script> 

this index.php //index page

<form name="form2">    <select name="regiones" onchange="showuser(this.value)">    <option>seleccione una region</option>      <?php         while($row = mysql_fetch_assoc($resultregion)){                         echo "<option value=\"".$row['idregion']."\">".$row['nombre']."</option><br/>";         }      ?>     </select> </form> <br /> <div id="mostrarclientes"><b>clients region</b></div> <div id="editarclientes"><b>form edit client</b></div> 

this vercliente.php //this first ajax call show clients in region

<?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db_clients") or die(mysql_error()); header("content-type: text/html; charset=windows-1252");  //////this function prints form edit (it's in return)     function repr($pair){     list($a, $b) = $pair; return "<form name=\"formedit$b\"> <div style=\"float:left; margin-right:3px\">&#149; $a</div> <div style=\"float:right\"> <input type=\"image\" name=\"edit\" src=\"edit.gif\" border=\"0\" title=\"editar\" value=\"$b\" onclick=\"edituser(this.value)\"></> <input type=\"image\" name=\"del\" src=\"delete.gif\" border=\"0\" title=\"eliminar\" value=\"$b\" onclick=\"if (!confirm('seguro que desea eliminar el registro \'$a\' ?')) {return false} else{deluser(this.value)}\"></> </div> </form>";  } ////////////////////  //////codigo para mostrar clientes   $numreg=$_get["ver"];  $nombreprovincia = ""; $nombrecomuna = ""; $nombregiro = ""; $nombrenombre = array();  $resultregion = mysql_query("select idregion, nombre region idregion = '$numreg'"); $titleregion= mysql_fetch_array($resultregion); $resultclientes = mysql_query("select nombre.idnombre idnombre, nombre.nombre nombrenombre, comuna.nombre nombrecomuna, giro.nombre nombregiro, provincia.nombre nombreprovincia, provincia.region_idregion idregion nombre inner join comuna on nombre.comuna_idcomuna = comuna.idcomuna inner join giro on nombre.giro_idgiro = giro.idgiro inner join provincia on comuna.provincia_idprovincia = provincia.idprovincia provincia.region_idregion = '$numreg' order nombreprovincia, nombrecomuna, nombregiro, nombrenombre");    $num_results = mysql_num_rows($resultclientes);  echo '<h2>'.$titleregion['nombre'].'</h2>';  if ($num_results > 0){       while ($row = mysql_fetch_assoc($resultclientes)) {         if ($nombreprovincia == $row['nombreprovincia']) {             if ($nombrecomuna == $row['nombrecomuna']) {                 if ($nombregiro == $row['nombregiro']) {                     $nombrenombre[] = $row['nombrenombre'];                     array_push($nombrenombre,$row['idnombre']);                 }                 else { //nombregiro                     $nombrenombre = array_chunk($nombrenombre, 2);                     $nombrenombre = array_map("repr", $nombrenombre);                     echo '<td style="background-color: #ccccff">' .implode('<br />', $nombrenombre).'';                     $nombregiro = $row['nombregiro'];                     echo '</ul></td></tr><tr><td style="background-color: #fff084">'.$nombregiro.'</td>';                     $nombrenombre = array($row['nombrenombre']);                     array_push($nombrenombre,$row['idnombre']);                 }             }             else { // nombrecomuna                 $nombrenombre = array_chunk($nombrenombre, 2);                 $nombrenombre = array_map("repr", $nombrenombre);                 echo '<td style="background-color: #ccccff">' . implode('<br />', $nombrenombre).'</td></tr>';                 $nombrecomuna = $row['nombrecomuna'];                 echo '<tr><td colspan="2" style="background-color: #ffcc00"><h4 style="margin: 0">'.$nombrecomuna.'</h4></td></tr>';                 $nombregiro = $row['nombregiro'];                 echo '<tr><td style="background-color: #fff084">'.$nombregiro.'</td>';                 $nombrenombre = array($row['nombrenombre']);                 array_push($nombrenombre,$row['idnombre']);             }         }         else { // nombreprovincia             if (!empty($nombrenombre)) {                 $nombrenombre = array_chunk($nombrenombre, 2);                 $nombrenombre = array_map("repr", $nombrenombre);                 echo '<td style="background-color: #ccccff">' . implode('<br />', $nombrenombre).'</td></tr></table>';             }              $nombreprovincia = $row['nombreprovincia'];             echo '<table cellspacing="1" style="background-color:#000000; float:left;margin-right:10px"><tr><td colspan="2" style="background-color: #ff6600; text-align:center"><h3 style="margin: 0">'.$nombreprovincia.'</h3></td></tr>';             $nombrecomuna = $row['nombrecomuna'];             echo '<tr><td colspan="2" style="background-color: #ffcc00"><h4 style="margin: 0">'.$nombrecomuna.'</h4></td></tr>';             $nombregiro = $row['nombregiro'];             echo '<tr><td style="background-color: #fff084">'.$nombregiro.'</td>';             $nombrenombre = array($row['nombrenombre']);             array_push($nombrenombre,$row['idnombre']);         }     }     if (!empty($nombrenombre)) {         $nombrenombre = array_chunk($nombrenombre, 2);         $nombrenombre = array_map("repr", $nombrenombre);         echo '<td style="background-color: #ccccff">' . implode('<br />', $nombrenombre).'</td></tr></table>';       } }         else{echo '<p>ningun dato fue encontrado aqui!</p>';}   ?> 

this editcliente.php // nested ajax call edit client info

<?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db_clients") or die(mysql_error()); header("content-type: text/html; charset=windows-1252");  $regedit=$_get["edit"];  //////dropbox results para editar   $resultcomunaedit = mysql_query("select idcomuna, nombre comuna order nombre asc"); $resultgiroedit = mysql_query("select idgiro, nombre giro order nombre asc"); ///////////////////// $editsql=mysql_query("select nombre.idnombre idnombre, comuna.nombre comuna, giro.nombre giro, nombre.nombre nombre nombre inner join comuna on nombre.comuna_idcomuna = comuna.idcomuna inner join giro on nombre.giro_idgiro = giro.idgiro idnombre = '$regedit'"); $rowedit = mysql_fetch_array($editsql);  $regname=$rowedit['nombre']; $regcomuna=$rowedit['comuna']; $reggiro=$rowedit['giro'];  echo '<h3>========================editar clientes====================================</h3>'; echo 'editando registro numero: '.$regedit.''; echo "<form name=\"editor\" method=\"post\" action=\"".$_server['php_self']."\"><div align=\"center\">"; echo "nombre cliente (max 16): <input name=\"nombrenuevo\" type=\"text\" maxlength=\"16\" size=\"25\" value=\"".$regname."\"></>"; echo "giro: "; echo "<input name=\"gironuevo\" type=\"text\" style=\"background-color:#cfcfcf\" maxlength=\"16\" size=\"15\" value=\"".$reggiro."\" readonly=\"readonly\"></>";  echo "<select name=\"giroedit\"><option value=\"\">cambiar a:</option>";     while($row = mysql_fetch_assoc($resultgiroedit)){         echo "<option value=\"".$row['idgiro']."\">".$row['nombre']."</option><br/>";     } echo "</select>";  echo "comuna: "; echo "<input name=\"comunanuevo\" type=\"text\" style=\"background-color:#cfcfcf\" maxlength=\"20\" size=\"15\" value=\"".$regcomuna."\" readonly=\"readonly\"></>";  echo "<select name=\"comunaedit\"><option value=\"\">cabiar a:</option>";        while($row = mysql_fetch_assoc($resultcomunaedit)){         echo "<option value=\"".$row['idcomuna']."\">".$row['nombre']."</option><br/>";     } echo "</select>";  echo "<input type=\"submit\" name=\"submiteditar\" value=\"guardar cambio\" onclick=\"return confirm('seguro que desea guardar cambios?');\"> </>"; echo "<input type=\"hidden\" value=\"".$regedit."\" name=\"idedit\" id=\"idedit\"/>"; echo "</div></form>";      ?> 

the first call work clients of region, when click on edit button goes wrong. hope can me, , sorry long question.

just in case wonder, when submit second form edit client info that's going post method not ajax.

the problem was using input type image, when clicked, refreshing site, nad onclick function wasnt loading, changed input type image button , worked =)


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -