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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看