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>
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.