php - Form Submit through JavaScript function

134

I'm using a Javascript function to submit a form. This function get all data from textbox, set the form action with this data and submit. The problem is that the url of the next page doesn't include faldone

<script type="text/javascript">
function update()
{
 var scatola=document.form2.scatola.value;
 var faldone=document.getElementById("faldone").innerText;
 var fascicolo=document.form2.fascicolo.value;
 var desc=document.form2.descrizione.value;
 var tipo=document.form2.tipodocumento.value;
 var annic=document.form2.annicon.value;
 var dal=document.form2.dal.value;
 var al="document.form2.al.value;
 var esd="document.form2.estremidal.value; 
 var esa="document.form2.estremial.value;
 var vec="document.form2.vecchiasegn.value;
 document.form2.action="aggiorna.php?scatola="+scatola+"&faldone="+faldone+"&fascicolo="+fascicolo+"&descrizione="+desc+"&tipod="+tipo+"&annicon="+annic+"&dal="+dal+"&al="+al+"&estremidal="+esd+"&estremial="+esa+"&vecchiasegn="+vec;
 document.form2.submit();
}
</script>
<body>
<?php 
 $conn=mysqli_connect("localhost","root","","barcode");
 $barcode=$_GET["codice"];
 $s="SELECT * from faldoni where faldone like '%$barcode%'";
 $q=mysqli_Query($conn,$s);?>
<form name="form2" action="" onSubmit="update()">
<table>
<?php
$r=mysqli_fetch_array($q);
echo"<tr><td><input type='text' name='scatola' value='$r[0]'></td>
<td id='faldone'>$r[1]></td>
<td><input type='text' name='fascicolo' value='$r[2]'></td>
<td><input type='text' name='descrizione' value='$r[3]'></td>
<td><input type='text' name='tipodocumento' value='$r[4]'></td>
<td><input type='text' name='annicon' value='$r[5]'></td>
<td><input type='text' name='dal' value='$r[6]'></td>
<td><input type='text' name='al' value='$r[7]'></td>
<td><input type='text' name='estremidal' value='$r[8]'></td>
<td><input type='text' name='estremial' value='$r[9]'></td>
<td><input type='text' name='vecchiasegn' value='$r[10]'></td></tr>
</table>";
<input type="reset" value="reset"><input type="submit" value="conferma">
 </form>
38

Answer

Solution:

You can achieve all you want by simply using theGET method and passing the value of faldone using ahidden input field.

<form name="form2" action="" method="GET">
  <table>
  <?php
    $r=mysqli_fetch_array($q);
    echo"
    <!-- use a hidden field to send the value of faldone -->
    <input type='hidden' name='faldone' value='$r[1]'>
    <tr><td><input type='text' name='scatola' value='$r[0]'></td>
    <td id='faldone'>$r[1]></td>
    <td><input type='text' name='fascicolo' value='$r[2]'></td>
    <td><input type='text' name='descrizione' value='$r[3]'></td>
    <td><input type='text' name='tipodocumento' value='$r[4]'></td>
    <td><input type='text' name='annicon' value='$r[5]'></td>
    <td><input type='text' name='dal' value='$r[6]'></td>
    <td><input type='text' name='al' value='$r[7]'></td>
    <td><input type='text' name='estremidal' value='$r[8]'></td>
    <td><input type='text' name='estremial' value='$r[9]'></td>
    <td><input type='text' name='vecchiasegn' value='$r[10]'></td></tr>
  </table>";
  <input type="reset" value="reset"><input type="submit" value="conferma">
 </form>
409

Answer

Solution:

Your code works fine but you have a few typo errors. The line

var al="document.form2.al.value;

has an extra " character as do the subsequent lines.

I have removed these and created a fiddle and it appears to work fine. It shows an alert box with the url of the post(get) and the elementfaldone is included with the correct data.

Here is a fiddle of the code.

fiddle

People are also looking for solutions to the problem: php - Laravel Passport API authentication fails testing

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.