3

I have two functions one is PHP second is JQuery. I want them to activate on same button click. But I want fisrt to activate PHP function and then JQuery. How can I do this. Here is my code. The problem is this line in JQuery function

var imagename = "<?php echo $nameoffile ?>";

When I replace that with hardcoded variable everything work fine. How can I control order of execution ?

HTML :

<form id ="submit_leave_email" enctype="multipart/form-data" method="POST" >
 <div class="form1">
 <div class="radio-line" id="radio-manager" style="font-size:12px">
 <input type="radio" id="rad-400" name="radio-manager" value="No"/> Registered
 <input type="radio" id="rad-400" name="radio-manager" value="No"/> Unregistered
       </div> <br>
 <h4 class="manager" style="font-size:12px">ID of car is :</h4><br><br> 
  <input type="hidden" name="MAX_FILE_SIZE" value="100000" class="input_1" />
  <input id="fileupload" name="uploadedfile" type="file" class="input_1" /><br/>
  <input id="brand" type="text" value="Name of car :"  class="input_1"         name="brand"  /><br></div> <br>
   <div id = "image" style="width:100px; height:100px"></div></div>
  <input id="submit"  type="submit" name="submit" value="submit">
</form>

PHP :

<?php
  if(isset($_POST['submit'])){
     $target_path = "images/";
     $nameoffile = basename( $_FILES['uploadedfile']['name']);
     $target_path = $target_path . $nameoffile; 
     echo $nameoffile;
  if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "There was an error uploading the file, please try again!";
     }
}
  ?>

JQuery :

<script type="text/javascript">
$( "#submit_leave_email" ).submit(function( event ) { 
     var brand = $('#brand').val();
     var id = localStorage.getItem("id");
      if($('#rad-400').is(':checked')){
        var ide = 'reg';
    } else {
        var ide = 'unreg';
    }
     var imagename = "<?php echo $nameoffile ?>";
     $.ajax({
            type: "POST",
            url:  "updateCarSql.php",
            data: "ide=" + ide + "&id="+ id + "&brand="+                         
            brand+"&imagename="+imagename,
            success: function(){
                $("#submit_leave_email").fadeOut();
                $("#update_success1").fadeIn();
            }
        });
});
</script>
cfhpanteracfh
  • 115
  • 2
  • 9
  • From where are you getting this PHP variable? As long as I can see this '$nameoffile' do not exist at moment you are calling it in the ajax. – Franco Dec 23 '15 at 14:17
  • I do not unterstand why you want to submit a form and an AJAX Request at the same time. What do you want to archieve with that? You can update your Database at the same time you upload the file. – Philipp Palmtag Dec 23 '15 at 14:27
  • There is vairable $nameoffile in php code on line 4. I'm trying to upload picture to server but if I use action="upload.php" it will lead me to that page and I don't want that. So I put code inside same page as form. And than I wanted to update database through the JQuery function. @Franco – cfhpanteracfh Dec 23 '15 at 15:27

1 Answers1

1

PHP code runs at the server, jQuery runs at the client so the strict answer to your question is ISSET is executed before the jQuery; however that doesn't really help you.

It looks like you are trying to use PHP to get the file name for your jQuery code before submitting the form. This is a bit of a catch 22 as PHP code won't be run until after you submit the form.

If you want to get the chosen file name you'll need to use javascript. Look at this question - Use jQuery to get the file input's selected filename without the path

Also look at preventDefault as I suspect you don't want to do an ajax call and submit your form.

Community
  • 1
  • 1
John C
  • 2,732
  • 2
  • 31
  • 46
  • Thank you. This answer helped me to understand what is going on. This is complete answer and this link was really helpful. @John C – cfhpanteracfh Dec 23 '15 at 15:48