简体   繁体   中英

JS/Ajax alert box error

I have an alert box that keeps prompting "Image uploaded", even though $imagename is empty.

Here's the script:

<script>
  function ajax_post1(ca){

    var cat = ca;
    var name = document.getElementById("name").value; 
    var desc = document.getElementById("description").value;
    var key = document.getElementById("keyword").value;
    var image = document.getElementById("image").value; 

    if ($.isEmptyObject(image)) {
      alert('pls upload your image')
    } else {
      alert(' image uploaded ')
    }

    var myData = 'content_ca='+ cat + '&content_desc='+desc+ '&content_key='+key+ '&content_name='+name;//build a post data structure

    jQuery.ajax({
      type: "POST", // HTTP method POST or GET
      url: "uploadsignuppackageresponse.php", //Where to make Ajax calls
      dataType:"text", // Data type, HTML, json etc.
      data:myData, //Form variables
      success:function(response){

      //$("#imagebox").append(response);
      //$("#contentText").val(''); //empty text field on successful
      //alert("haha");

      }, error:function (xhr, ajaxOptions, thrownError){
        alert(thrownError);
      }
    });

  };
</script>

This is the main page:

<?php
  $sql1 = mysql_query ("SELECT * FROM dumimage WHERE email = '$user_signup' AND cat='company' ");
  $row = mysql_fetch_array($sql1);
  $imagename = $row['name'];
?>

Name: 
  <input id="name" type="text"  ></input>
  <input id="image" type="hidden" value="<?php echo $imagename ?> "></input>
Description
  <textarea id="description" rows="7" cols="42"></textarea>
Keywords: 
  <input id="keyword" type="text" placeholder="3 Maximum Keywords" ></input>

<input type="submit"  value="Upload" class="pre" style="float:left; onClick="ajax_post1('company')">   

Try this to see if your objects empty

if (image.length < 1) {
      alert('pls upload your image')
} else {
      alert(' image uploaded ')
}

Try to replace this line:

if ($.isEmptyObject(image)) {

With this one:

if (image != '') {

You also have to correct your php code because you have closed the bracket in the wrong place and you are missing a semicolon:

<input id="image" type="hidden" value="<?php echo $imagename;?>"></input>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM