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 7080251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:46:20+00:00 2026-05-28T06:46:20+00:00

Apologies if this has been answered before (I couldn’t find the answer when I

  • 0

Apologies if this has been answered before (I couldn’t find the answer when I searched the archives)

I’ve got a page protected by a password:

<?php

if($_POST['pw'] == 'pw')
{
    //Page content
} else
{
    //Display password form
}
?>

Within the page content, I’ve got another form, which I want to submit using jQuery, and have the following code:

<script type='text/javascript'>

  var dataString = $('input#input1').val();  

  $(function() {  

  $('#submit').click(function()
  {  
    $.ajax({  
      type: 'POST',  
      url: 'p2.php',  
      data: dataString, 
      dataType: html,
      success: function(data2) {
        $('#testResult').html(data2);
      }  
    });  
    return false; 
  });
});

</script>

<form name='form1' id='form1' action=''>  
  <fieldset>  
    <label for='input1' id='input1_label'>Input 1</label>  
    <input type='text' name='input1' id='input1' size='30' />  

    <input type='submit' value='Update / reset' id='submit' class='buttons' />
   </fieldset>  
</form> 

<div id='#testResult'></div>;

However, clicking submit then sends the form to p1.php?input1=test (i.e., the data string is being sent to p1.php, not p2.php). If I edit the code and remove dataType:html and the 2 references of data2, then this doesn’t happen (infact, nothing happens, so I assume that jQuery is submitting the data to the form). I’ve also changed the type to ‘GET’, incase the 2 POST requests on the same page were causing problems, but this didn’t change the result.

What am I missing to get the information from p2.php (i.e. data2) and displaying it?!

EDIT

Thanks to a comment pointing out a typo, I’ve changed dataType: html to dataType: 'html' – this now doesn’t cause the page to redirect to p1.php?input1=test, but once again, it doesn’t do anything (when it should still be returning the value of data2)

EDIT 2

I’ve updated the code so dataString is now:

var dataString = $('input#input1').val();   
dataString = 'var1='+dataString;

but this hasn’t made any difference

For clarification, my p2.php just contains the following:

<?php

  echo "<p>HELLO!</p>";

?>

EDIT 3

I made the changes to my code has suggested by Damien below; I get the alert of “works!” but still nothing seems to be returned from p2.php, and nothing is inserted into the #testResult div.

  • 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-05-28T06:46:20+00:00Added an answer on May 28, 2026 at 6:46 am
    $(function() {  
      $('#submit').click(function()
      {  
        var dataString = $('#form1').serialize();
        $.ajax({  
          type: 'POST',  
          url: 'p2.php',  
          data: dataString, 
          success: function(data2) {
            alert('works!');  // ADDED AFTER UPDATE
            $('#testResult').html(data2);
          },
          /* ADDED AFTER UPDATE */
          error:function(obj,status,error)
          {
             alert(error);
          }
        });  
        return false; 
      });
    });
    

    Edit:
    In p2.php:

    <?php
    var_dump($_POST['pw']);
    ?>
    

    In p2.php you then need to output ( using echo, for example) what you want to be returned as ‘data2’ in your ajax success call.

    UPDATE:

    Since you’re Ajax request fires succesfully, that means either your post is not passed correctly, or you’re not outputting anything. I’ve re-looked at your code and I saw this:

    <input type='text' name='input1' id='input1' size='30' />  
    

    that means you’re fetching the wrong $_POST variable!

    Do this:

    1. Since you’re sending a name=”input1″, in your p2.php try with:

      <?php
      if(isset($_POST['input1'])
      {
       echo $_POST['input1'];
      }
      else
      {
       echo 'No post variable!';
      }
      

      And in your jquery success:

      success: function(data2) {
             alert(data2);  
             $('#testResult').html(data2);
           },
      
    2. That oughta work, if you follow it literally. In the remote possibility it won’t work, forget AJAX, remove the javascript and do a normal post submitting with p2.php as an action of your form 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies if this has been asked before (I couldn't find the answer anywhere), but
Apologies if this has been asked before. I searched and couldn't find it. In
Apologies if this has been answered before, but I find it very difficult to
Apologies if this has been answered before but I can't find a reference. I
My apologies if this has been answered before or is obvious...did some searching here
Apologies if this has been answered before. I've been trying to construct an XPath
Apologies if this has been answered before or if it's too obvious - pointers
Apologies if this question has been answered before. Though the answers I've seen look
Apologies if this has been answered elsewhere, but I have yet to find a
Apologies if this question has been asked. I couldn't find it, but if it

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.