Error message in JS/Ajax alert box

I keep receiving an alert box saying "Image uploaded" even when the variable $imagename is empty.

Below is the script in question:

<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('Please upload your image')
    } else {
      alert('Image uploaded')
    }

    var myData = 'content_ca='+ cat + '&content_desc='+desc+ '&content_key='+key+ '&content_name='+name;

    jQuery.ajax({
      type: "POST",
      url: "uploadsignuppackageresponse.php",
      dataType:"text",
      data:myData,
      success:function(response){

      //$("#imagebox").append(response);
      //$("#contentText").val('');
      //alert("haha");

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

  };
</script>

This snippet shows the main page content:

<?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')">   

Answer №1

Check if your objects are empty by giving this a shot

if (items.length === 0) {
      alert('No items found, please add some!')
} else {
      alert('Items successfully added')
}

Answer №2

Consider updating this line:

if ($.isEmptyObject(image)) {

To the following:

if (image != '') {

Furthermore, make sure to fix your PHP code by rearranging the bracket and adding a missing semicolon:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Bootstrap tab content getting shifted downwards

Having an issue with the Tab plugin in Bootstrap on a particular page. The tab body is being pushed down 400px below the actual tabs. This behavior is only occurring on this specific page, while most other pages using the same plugin are functioning fine. ...

How can I display and utilize the selected value from a Rails select form before submitting it?

Currently, I am in the process of developing a multi-step form for placing orders. This form includes two dropdown selectors - one for shipping countries and another for shipping services. Once a country is selected, all available shipping services for tha ...

Storing segments of URL data for future use in React applications

Is there a way to extract information from a URL? I wish to utilize the appended details in this URL http://localhost:3000/transaction?transactionId=72U8ALPE within a fetch API. My goal is to retrieve the value 72U8ALPE and store it either in a state var ...

Unable to make an ajax request due to github.io's restrictions

I am facing an issue with all my apps that have ajax requests, as they are returning errors stating: "This request has been blocked; the content must be served over HTTPS." An example of this error can be seen at https://zzharuk.github.io/local_weather_w ...

Guide on modifying values using both input fields and buttons at the same time

I am facing an issue with my input field that is designed to accept numbers. I want the value to be changed both via keyboard input and buttons. The buttons are functioning correctly unless I try to use keyboard input as well. For instance, if I enter 10 ...

Discovering whether the incoming request to an AngularJS application is a GET or POST request can be efficiently accomplished

I recently started learning angularjs and I successfully built an application using the angular seed skeleton https://github.com/angular/angular-seed for the client side. For my API server, I utilized expressjs server. The URL of my home page is localhost ...

Retrieving a specific data point from the web address

What is the most efficient way to retrieve values from the window.location.href? For instance, consider this sample URL: http://localhost:3000/brand/1/brandCategory/3. The structure of the route remains consistent, with only the numbers varying based on u ...

Extract the content of a textbox within an iframe located in the parent window

Is it possible to retrieve the value of a text box in an iframe from the parent page upon clicking a button? Below is an example code snippet showcasing the situation: <div> <iframe src="test.html" > <input type=text id="parent_text"> & ...

What is the advantage of not importing related modules?

As a newcomer to React, please excuse any novice questions I may have. I am currently utilizing npx create-react-app to develop a React app, but I'm unsure of the inner workings: Q1-If I were to throw an error in a component like so: import React, { ...

Create a full-width slider using Materialize CSS framework

When using materializecss to create a slider, I encountered an issue where the image is full width but not full height, causing scrollbars to appear. What changes can I make to ensure the slider fills out my screen without any scrollbars? Additionally, I ...

Retrieve a file from an Express API using React with the help of Axios

When working with a React client and an Express API, how can the React client download a file sent by the Express API? Issue: If I manually enter the URL into my browser's address bar and hit enter, the file downloads successfully. However, when I ...

What is the best way to transform URL images on a webpage into media library images in WordPress?

Having trouble converting HTML Static Sites to WordPress because images are hosted through HTML SRC Code. After conversion, no one can see the images in visual format on pages during editing. Is there a way to convert all image links inside pages to media ...

Symfony 4 Forms: Enhancing User Experience with Prototypes

Recently, I've delved into Symfony 4 and encountered an issue with rendering a form featuring a ChoiceType Field with numeric choices. The goal is to allow the user to select a specific number of tags. Here's a snippet from my controller: class ...

What is the process for incorporating JavaScript files into an Angular project?

I have a template that works perfectly fine on Visual Studio. However, when I try to use it on my Angular project, I encounter an issue with the JavaScript code. I have filled the app.component.html file with the corresponding HTML code and imported the ...

CSS with three columns: the middle column has a fixed size, while the right and left columns

I am attempting to create a 3 column layout with specific requirements: The middle column is fixed at a width of 660px The left and right columns should each take up half of the remaining space, but have a minimum width of 120px The middle div need ...

Is it possible to eliminate the dedupe feature in npm?

By mistake, I accidentally ran the command npm dedupe and now all of my node_modules directories are flattened. While this reduces file size, it's making it more difficult to find things. Is there a way to reverse this and return to the hierarchical f ...

Using VueJs's createElement() to dynamically insert HTML content

I am currently working on a component that converts all icons to SVG format. By the end of my code, I have included the following: return createElement('i', '<SVG>CODE</SVG>' ) In the spot where the SPA ...

Determine if the value is present in every element of the array and return true

I am looking for a way to determine if all products have the status "Done" in their respective statusLog arrays. If any product does not contain "Done" or lacks the statusLog property altogether, then the function should return false. Although the current ...

Tips on choosing a child element with a parameter in React

Is it possible to pass a parameter in my function and use it to select a child of my JSON parse? I want to create a function (checkMatch) that can check if a username in my database matches with the input value. It should return 1 if there is a match, oth ...

Discover the method to activate the back button feature on ajax pages

I am currently working on a website that is navigated in the following way: $(document).ready(function () { if (!$('#ajax').length) { // Checking if index.html has been loaded. If not, navigate to index.html and load the hash part with ajax. ...