0

I'm sending a variable from a form using AJAX and this variable is proccess and sends to a PHP file that makes a MySQL query. The value of the input text is setting when I click in a row of one table. When I click the button to send the variable it returns the result of the mysql query. Well, if I click one more time in the button when the value of the input is other the query isn't modify, it returns me the first query. I need to refresh that query and I think that it's possible if I clear the variable. I put here the code of all. Thanks.

HTML Code

<td><input type="hidden" id="prueba" name="prueba"/></td>
<script type="text/javascript">
    var valor=document.getElementById("prueba").value;
</script>
<td><input type="button" name="ver" onclick="load(valor)" value="Ver linea seleccionada"/></td>

AJAX code

function load(str)
{
    var xmlhttp;
    if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    }
    else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("mus").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST","mostralle.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("q="+str);
}

PHP Code (mostralle.php)

include"conexion.php";
$q=$_POST['q'];
mysql_connect($servidor, $usuario, $clave)or die (mysql_errno().mysql_error());
mysql_select_db($basedatos)or die (mysql_errno().mysql_error());
$result=mysql_query("SELECT Serie, Numeroas, Numlinea, Codart, Cant, Descripcion, Precio, Tipoiva, Subtotal FROM PRESUP_D WHERE Serie='$q' OR Numeroas='$q'");
while($row=mysql_fetch_row($result)){
    echo "<tr>";
    for($i=0;$i<mysql_num_fields($result);$i++){
        echo "<td>$row[$i]</td>";
    }
    echo "</tr>";
}
mysql_close();
temerariomalaga
  • 87
  • 3
  • 10
  • change `id="prueba"` to `class="prueba"` and get closest value. Id is unique, so it always returns the first one – Linga Mar 27 '14 at 09:55
  • I change that and get the value var valor=document.getElementsByClassName("prueba").value; but it returns the value undefined. – temerariomalaga Mar 27 '14 at 10:04
  • You tagged this question with jQuery, so why aren't you using jQuery? It would certainly simplify your code (half the lines and half the problems) :) – iCollect.it Ltd Mar 27 '14 at 10:11
  • I'm too bad in javascript and worst in JQuery haha this the reason that I don't use that. – temerariomalaga Mar 27 '14 at 10:13
  • read http://stackoverflow.com/questions/3808808/how-to-get-element-by-class-in-javascript , http://stackoverflow.com/questions/17965956/how-to-get-element-by-class-name – Linga Mar 27 '14 at 10:22
  • this links explain how to clear the input or get the element by class but I need to clear the variable. – temerariomalaga Mar 27 '14 at 11:00

0 Answers0