Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7885445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:04:22+00:00 2026-06-03T05:04:22+00:00

Trying to practice ajax and PHP a little, I can’t figure out why whenever

  • 0

Trying to practice ajax and PHP a little, I can’t figure out why whenever I send something, it returns empty or undefined index.

PHP:

<?php

    if (!empty($_POST['message'])) {
        echo 'works';
    } else {
       echo 'empty';
    }

?>

returns empty.

JS:

$('form').submit(function(){

    var meth = $(this).attr('action');

    var msg = $('.msg');
    $.post(meth, function(data) {
        $('.messages').append(data);
    }) ;

    return false; 
}); 

HTML:

<form class="send" action="process.php" method="post">
    <textarea type="text" name="message" class="msg" value="blah"></textarea>
    <input type="submit" name="submit" class="submit">
</form>

If I just do ‘echo 'test'; in the process.php file, it works; undefined index if otherwise.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:04:24+00:00Added an answer on June 3, 2026 at 5:04 am

    I see you’ve edited the syntax errors, so you can ignore this section:

    Well of the top of my head I see two syntax errors, one in php:

    <?php
    
    
      if (!empty($_POST['message'])) {
        echo 'works';
      } else [ // <- This should be a {
         echo 'empty';
      }
    
    ?>
    

    and one in JS:

    $('form').submit(function(){
    
          var meth = $(this).attr('action');
    
          var msg = $('.msg');
          $.post(meth, function(data) {
    
            $('.messages').append(data);
    
          }) // <- Should be a ; here
    
          return false; 
        });
    

    Also let me be the first to point out that you should indent your code properly. It’s hard to see the depth of your scope otherwise, and just makes your code plain unreadable.

    empty is the wrong function to be using to check if an index on an array is set. That’s what’s generating your “undefined index” warnings. Use isset or array_key_exists instead:

    if (!isset($_POST['message'])) {
    // or
    if (!array_key_exists('message', $_POST)) {
    

    The empty function just checks if the variable contains a value that evaluates to empty, such as an empty string, an empty array, a NULL etc. It doesn’t check if a variable is set.


    Additionally, you don’t set the value of a textarea by populating the value attribute. That won’t work. You set the value by placing it between the open and close tags like this:

    <textarea class="msg">My message</textarea>
    

    Then you fetch the text (in jQuery) by requesting the value

    var my_message = $('textarea.msg').val()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how this would be done in practice, so as
I am trying to make a Ruby on Rails post via AJAX, and can't
Im trying to find a best practice to load usercontrols using Ajax. My first
I've been trying to practice making sliding menus with slidetoggle, slidedown/up...etc but I can't
Recently I was trying some practice programs in python and I came across this
I'm trying to practice writing better code, so I wanted to validate my input
I'm trying to practice my F# by writing small console scripts in F# in
Why is my sql injection which i am trying to practice on not working.
I am fairly new to vim. I am trying to practice (been reading a
I'm using Eclipse with C++ plugins on my macbook, trying some practice projects to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.