Pregunta

Am trying to pass a Multi Checked Values into a PHP file that creates a PDF file.

Here my HTML code for the form:

<tr>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Radiography (RT)" /></td>
    <td style="padding-left:4px;">Radiography (RT)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Ultrasonic (UT)" /></td>
    <td style="padding-left:4px;">Ultrasonic (UT)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Liquid Penetrant (PT)" /></td>
    <td style="padding-left:4px;">Liquid Penetrant (PT)</td>
</tr>
<tr>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Magnetic Particle (MT)" /></td>
    <td style="padding-left:4px;">Magnetic Particle (MT)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Visual (VT)" /></td>
    <td valign="middle">Visual (VT)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Leak (LT)" /></td>
    <td style="padding-left:4px;">Leak (LT)</td>
</tr>
<tr>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Eddy Current (EC)" /></td>
    <td style="padding-left:4px;">Eddy Current (EC)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Infrared &amp; Thermography (I&amp;T)" /></td>
    <td style="padding-left:4px;">Infrared &amp; Thermography (I&amp;T)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Acoustic Emission (AE)" /></td>
    <td style="padding-left:4px;">Acoustic Emission (AE)</td>
</tr>

Here I use some validation and pass the values to my variable:

var checkBoxes=document.getElementsByName("afchek[]");

var booleanResult=false;
for(var i=0;i<checkBoxes.length;i++){
    if(checkBoxes[i].checked){
    booleanResult=true;
    break;
}
}

if(booleanResult==false){
    showError($afchek);
ok = false;
}

And here I'm passing my values to another php file that creates a pdf file:

$.ajax({
type: 'POST',
url: 'pdf/create_ASNT.php',
data: 'afname=' + $afname.val() + '&affname=' + $affname.val() + '&afdob=' + $afdob.val() + '&afage=' + $afage.val() + '&afgender=' + gender + '&afresaddress=' + $afresaddress.val() + '&afresmobile=' + $afresmobile.val() + '&afresemail=' + $afresemail.val() + '&afstat=' + $afstat.val() + '&afmobile=' + $afmobile.val() + '&afemail=' + $afemail.val() + '&afchek[]=' + checkBoxes + '&afenroll=' + $afenroll.val() + '&aftimecontact=' + $aftimecontact.val() + '&page1=ASNT-NDT-form.php',
success: function(){
    $afsuccess.fadeIn();
    $aferror.fadeOut();
}

Here I'm retrieving the query string values:

$afname=ucfirst($_REQUEST["afname"]);
$affname=ucfirst($_REQUEST["affname"]);
$afdob=ucfirst($_REQUEST["afdob"]);
$afage=ucfirst($_REQUEST["afage"]);
$afgender=ucfirst($_REQUEST["afgender"]);
$afresaddress=ucfirst($_REQUEST["afresaddress"]);
$afresmobile=ucfirst($_REQUEST["afresmobile"]);
$afresemail=ucfirst($_REQUEST["afresemail"]);
$afstat=ucfirst($_REQUEST["afstat"]);
$afmobile=ucfirst($_REQUEST["afmobile"]);
$afemail=ucfirst($_REQUEST["afemail"]);
//our problem here
$afchek=ucfirst($_REQUEST["afchek"]);
$afenroll=ucfirst($_REQUEST["afenroll"]);
$aftimecontact=ucfirst($_REQUEST["aftimecontact"]);

Now I could not print the values that I just passed from that file. I tried all the below things...

for($i=0;$i<count($afchek);$i++) {
    //    $selected_colors=$selected_colors . $colors[$i] . " , ";
$pdf->Cell(54, 0, $afchek[$i]);
}

$acco = count($afchek);
$pdf->Cell(54, 0, $acco);

$acco = count($_GET["afchek"]);
$pdf->Cell(54, 0, $acco1);

foreach($_GET["afchek"] as $valuer) {
$pdf->Cell(54, 0, $valuer);
}

Please can someone help to sort out this issue.

¿Fue útil?

Solución

I don't know what you are trying to achieve but here is the working example: This will print the value you have submitted in your PHP file. Also try to use jquery instead.

<form action="formaction.php" id="form">
<table>
<tr>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Radiography (RT)" /></td>
    <td style="padding-left:4px;">Radiography (RT)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Ultrasonic (UT)" /></td>
    <td style="padding-left:4px;">Ultrasonic (UT)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Liquid Penetrant (PT)" /></td>
    <td style="padding-left:4px;">Liquid Penetrant (PT)</td>
</tr>
<tr>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Magnetic Particle (MT)" /></td>
    <td style="padding-left:4px;">Magnetic Particle (MT)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Visual (VT)" /></td>
    <td valign="middle">Visual (VT)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Leak (LT)" /></td>
    <td style="padding-left:4px;">Leak (LT)</td>
</tr>
<tr>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Eddy Current (EC)" /></td>
    <td style="padding-left:4px;">Eddy Current (EC)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Infrared &amp; Thermography (I&amp;T)" /></td>
    <td style="padding-left:4px;">Infrared &amp; Thermography (I&amp;T)</td>
    <td style="padding-left:4px;"><input style="margin-top:0px; cursor:pointer;" type="checkbox" name="afchek[]" value=" Acoustic Emission (AE)" /></td>
    <td style="padding-left:4px;">Acoustic Emission (AE)</td>
</tr>
<tr>
    <td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="main.js"></script>

Here is how you submit the form and it will alert the value you have submitted.

$(document).ready(function(){
    $("#form").on("submit",function(event){
           event.preventDefault();
           postdata = $('#form').serialize();
           $.ajax({
             type: 'POST',
             url: 'formaction.php',
             data: postdata,
             success: function(res){
                alert(res);
             }
        });
    });
});

Here is your PHP file: Which return whatever has been submitted through the form and alert it alert on success of your ajax request.

<?php 
    //formaction.php
    $afname=$_POST["afchek"];
    foreach ($afname as $key => $afname) {
       echo ucfirst($afname);
    }
?>

Now you can use this value the way you want.

Demo link: http://dharmeshpatel.net/core_reporting_api/test.php

Hope that helps. Not exactly the way you wanted but you will get an idea.

Otros consejos

Use POST supper global variable instead.

e.g.

ucfirst($_POST["afname"]);

or, try to serialize data and and pass that to your php file.

something like:

$("#Your_Form_ID").on("submit",function(){
   postdata = $('#Your_Form_ID').serialize();
   $.ajax({
     type: 'POST',
     url: 'pdf/create_ASNT.php',
     data: postdata ,
     success: function(res){
         $afsuccess.fadeIn();
         $aferror.fadeOut();
     }
 });

In your PHP file:

    $afname=ucfirst($_POST["afname"]);

Hope that helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top