0

Following the transition from a Windows environment to Linux and modernise a code. I meet a new problem. I send a csv file. But when i click on the button in order to handle it, nothing happens

<?php
echo "<form enctype=\"multipart/form-data\" id=\"form_impmat\" 
name=\"form_impmat\" action=\"\" method=\"post\">\n";
....
echo "<p><input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"1000000\" 
/>\n
            Fichier au format CSV : <input name=\"userfile\" type=\"file\" 
style=\"border:1px solid #CCCCCC;\" />\n
            <a href=\"javascript:valid_form();\" title=\"Envoyer le 
fichier\" class=\"submit\">Envoyer le fichier</a></p>\n</form>";
?>

I think that the problem is in the javascript :

<script type="text/javascript">

function valid_form() {

if(document.layers) {
    form = document.layers('form_impmat');
}
//IE6
if(document.all) {
    form = document.all('form_impmat') ;
}
//Netscape 6
if(!document.all && document.getElementById) {
    form = document.getElementById('form_impmat') ;
}

var valid = false;

if (form.ORDER_DETAILS_ID.length != null) {
    for (var i=0 ; i < form.ORDER_DETAILS_ID.length ; i++) {

        if (form.ORDER_DETAILS_ID[i].checked) {

            valid = true;
        }
    }
} else {
    if(form.ORDER_DETAILS_ID.checked)
        valid = true;
}


var message = "";
if (form.userfile.value == "") message += "Vous n'avez spécifié aucun 
fichier !\n";
if(!valid) message += "Vous n\'avez pas choisi de commande !\n";

if (message != "") alert('Opération annulée!\n\n'+message);
else form.submit();


}
</script>

Nothing happens but in the old version, the csv file is handled

talou
  • 1
  • 1
  • why are you echoing html ? you can write it directly... if you dont have any php script get rid of the php tags and write the html directly. – anees Jan 30 '19 at 21:05
  • 1
    `document.layers` I haven't seen that for a while, it's like seeing an old friend again. – Matthew Page Jan 30 '19 at 21:07
  • Transitioning from Windows to Linux shouldn't affect what happens with PHP and JavaScript, btw, but see this: https://www.w3schools.com/php/php_file_upload.asp Upoloading a file and handling the uploaded file are really two separate events. Be sure to treat them like that. First, upload the file. Check it has uploaded. Then do any processing on the upoaded file. Check the processing has worked. – Hektor Jan 30 '19 at 21:09

1 Answers1

0

You're trying to include a string on two lines but JavaScript does not support that. Change

if (form.userfile.value == "") message += "Vous n'avez spécifié aucun 
fichier !\n";

to

if (form.userfile.value == ""){
    message += "Vous n'avez spécifié aucun fichier !\n";
}

In future, you can figure out whether the request is sending or it is a JavaScript error by right clicking the webpage and viewing "inspect element" as in Chrome or something similar. It shows you if there are any JavaScript errors and what, if anything, JavaScript sent to a server.

If you want to get advanced, there is also this: Creating multiline strings in JavaScript