-1

Hey guys i am using this script to make dynamic webform (add new input if needed).

<form method="post" action="">
 <table class="dd" width="100%" id="data">
  <tr>
    <td>Vin</td>
    <td width="99%"><input name="vin1" id="vin1" type="text" /></td>
    </tr>
</table>
<input name="sub" type="submit"  value="Submit values"/>
<input type="button" id="addnew" name="addnew" value="Add new item" /> 
<input type="hidden" id="items" name="items" value="1" />
</form> 
<?php var_dump ($_POST); ?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
//<![CDATA[
 $(document).ready(function() {
  var currentItem = 1;
  $('#addnew').click(function(){
   currentItem++;
   $('#items').val(currentItem);
var strToAdd = '<tr><td>Vin</td><td width="17%"><input name="vin'+currentItem+'" id="vin'+currentItem+'" type="text" /></td></tr>';
   $('#data').append(strToAdd);

  });
 });

//]]>
</script>

It works well and also name each input with number so in my case its : vin1, vin2, vin3... Problem is i am not sure how to tell PHP to process this data. Because i am using something like:

 public function fetchByVinEvidence() {
     $success = false;
     try{
        $con = new PDO( DB_HOST, DB_USER, DB_PASS ); 
        $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sql = "SELECT * FROM evidence_calculations WHERE vin = :vin LIMIT 5";
        $stmt = $con->prepare( $sql );
        $stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
        $stmt->execute();
            while ($row = $stmt->fetch()){
                echo "<tr>";
                echo "<td>#4</td>";
                echo "<td>".$row['street']."</td>";
                echo "<td>".$row['city']."</td>";
                echo "<td>".$row['claim_number']."</td>";
                echo "<td>".$row['country']."</td>";
                echo "<td><a href =\"javascript:void(0)\" onclick = \"document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\">detail</a></td>";
                echo "</tr>";
            }
            }catch(PDOExeption $e){
            echo $e->getMessage();
            echo $con->errorInfo();
            }

         return $success; 
     }

For a single input but how can i edit it for multiple input, i am new in PHP OOP so i am really not sure how to make proper cycle, how to limit everything to not let user to send milion things to database at once and so on. Can you guys help me a bit to edit my PHP script to be able to work with data from multiple form?

Thanks!

1

1 Answer 1

5

instead of numbering the input names just append [] to the end of vin, when submitted php will automatically take all the inputs that have that name and load each value into an array

HTML sample

<input name="vin[]" type="text" />
<input name="vin[]" type="text" />
<input name="vin[]" type="text" />
<input name="vin[]" type="text" />

PHP

public $vin = array();
public function __construct( $data = array() ) {
     if( isset( $data['vin'] ) ) {
          foreach($data['vin'] as $index=>$vin){
                $this->vin[$index] = stripslashes( strip_tags( $vin ) );     
          }
     }
}
//..
$whereQuery = array();
foreach($this->vin as $index=>$vin){
   $whereQuery[] = "vin= :vin".$index;
}
$sql = "SELECT * FROM evidence_calculations WHERE ".implode(" OR ",$whereQuery)." LIMIT 5";
//...
foreach(array_keys($this->vin) as $index){
   $stmt->bindValue( "vin".$index, $this->vin[$index], PDO::PARAM_STR );
}
Sign up to request clarification or add additional context in comments.

10 Comments

And how can i check full aray if every part of it exist in database and display different result for each of it?
@MartinFerko, edited, something like that should work
Becuase i can do something like $vinArray[1], $vinArray[2] but i dont know how many inputs user use
you can get the length of an array by using count(): $numInputs = count($vinArray)
I am using something like this on start of my class, can you check your code a bit to help it work for me? public $vin = null; public function __construct( $data = array() ) { if( isset( $data['vin'] ) ) $this->vin = stripslashes( strip_tags( $data['vin'] ) ); } public function storeFormValues( $params ) { $this->__construct( $params ); } pastebin.com/J7xQNyDt
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.