douhuang2673 2015-12-23 14:03
浏览 36
已采纳

执行顺序ISSET vs JQUERY

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>
  • 写回答

1条回答 默认 最新

  • dsxd62219570 2015-12-23 14:31
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含