1

I am trying to post file and button value and process it in php.

Issue is in my php script ha2e.php condition if (isset($_FILES['file'])) {} always goes false

html:

<div id="htmlar2en" style="text-align:center">
            <div class="container">
            <div style="text-align:center">
            <h1>  Arabic to English HTML </h1>
            <form method="post" enctype="multipart/form-data" target="iframe4" id = "htmla2e">
            *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" id="submit4" name="submit4" value="HTML Arabic to English" />
            </form>
            <iframe name="iframe4" id="iframe4" src="" style="display:none" ></iframe>
        </div>
       </div>
    </div>

jquery:

$(document).ready(function() {

   var extra_data = "This is more stuff to send";
   var Bvalue = $(this).attr("value");

   $('#submit4').click(function() {
  $.ajax({
     type: "POST",
     url: "he2a.php",
     data: {'form': $("#htmla2e").serialize(), 'other': extra_data, 'submit4': Bvalue},
     success: function(msg) {
        alert("Form Submitted: " + msg);
     }
  });

   });

});

php:

    <?php

        if (isset($_FILES['file'])) {
            header('Content-Type: text/html; charset=UTF-8');
            echo '<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />';
            require_once "simplexlsx.class.php";
            require '../../Arabic.php';
            $Arabic = new I18N_Arabic('Transliteration');
            $xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
            echo "df";
            echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';
            $eng = array();
            list($cols,) = $xlsx->dimension();  
            foreach( $xlsx->rows() as $k => $r) {
        //      if ($k == 0) continue; // skip first row
                echo '<tr>';
                for( $i = 0; $i < $cols; $i++)
                {   
                    if ($_POST['submit3'] == 'HTML English to Arabic')
                    {
                        $temp =  $Arabic->en2ar($r[$i]);                
                }
                    else if ($_POST['submit4'] == 'HTML Arabic to Englis')
                    {
                        $temp =  $Arabic->ar2en($r[$i]);                
                    }
                    else 
                        continue;
                    echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
                }
                echo '</tr>';
            }
            echo '</table>';
        }
        ?>
user123
  • 5,269
  • 16
  • 73
  • 121
  • Please check this demo And download it .and modify it . http://www.sanwebe.com/downloads/18-ajax-file-upload – Pratik Joshi Jul 08 '14 at 07:10
  • Read here http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery => You cannot practically do Ajax file uploads if you need wide-ranging browser support. – Pratik Joshi Jul 08 '14 at 07:16

2 Answers2

1

when you use the ajax request, of course you the $_FILES is empty.

$('#submit4').click(function() {
  $.ajax({
     type: "POST",
     url: "he2a.php",
     data: {'form': $("#htmla2e").serialize(), 'other': extra_data, 'submit4': Bvalue},
     success: function(msg) {
        alert("Form Submitted: " + msg);
     }
 });

you should assgin the form action to the url you want post data to, for eample ha2e.php, fllow is the code.

<form method="post" enctype="multipart/form-data" target="iframe4" id = "htmla2e" action="ha2e.php">
    *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" id="submit4" name="submit4" value="HTML Arabic to English" />
</form>

at last we use chrome developer tools we will see we post the file to the action ha2e.php, if action is empty, the default current url page. enter image description here

jediliang
  • 19
  • 2
  • in action and `jquery` both we need to give file name? – user123 Jul 08 '14 at 07:08
  • @user123 Read here http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery => You cannot practically do Ajax file uploads if you need wide-ranging browser support. – Pratik Joshi Jul 08 '14 at 07:16
1

You may want to see the answers on this post or on this - uploading files with AJAX is not working with the generic method you are using.

Community
  • 1
  • 1
Barnabas Kecskes
  • 1,861
  • 17
  • 24
  • 1
    Read here http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery => You cannot practically do Ajax file uploads if you need wide-ranging browser support. – Pratik Joshi Jul 08 '14 at 07:16
  • @PratikJoshi: then what is the solution for this? – user123 Jul 09 '14 at 02:57
  • @user123 , I already told in Your Question comments, you need to use Plugin "Please check this demo And download it .and modify it . sanwebe.com/downloads/18-ajax-file-upload" – Pratik Joshi Jul 09 '14 at 03:39