Sorry if the answer was previously given (I could not find the answer when I searched the archives)
I have a password protected page:
<?php if($_POST['pw'] == 'pw') {
Inside the contents of the page, I have another form that I want to submit using jQuery, and I 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 the submit button then submits the form to p1.php? input1 = test (i.e. a data string is sent to p1.php, not p2.php). If I edit the code and delete the dataType:html and 2 data2 links, this will not happen (infact, nothing happens, so I assume jQuery sends the data to the form). I also changed type to "GET" if 2 POST requests on the same page caused problems, but that did not change the result.
What am I missing to get information from p2.php (i.e. data2 ) and display it ?!
EDIT
Thanks to the comment pointing to a typo, I changed dataType: html to dataType: 'html' - now this does not lead to redirecting the page to p1.php? input1 = test, but again, t do something (when it should still return the value of data2 )
EDIT 2
I updated the code, so dataString now:
var dataString = $('input#input1').val(); dataString = 'var1='+dataString;
but it didnβt matter.
For clarification, my p2.php just contains the following:
<?php echo "<p>HELLO!</p>"; ?>
EDIT 3
I made changes to my code suggested by Damien below; I get a warning about "work!". but still nothing is returned from p2.php, and nothing is inserted into the #testResult div.