0

I have the following ajax function that is called from a form button that gets the number used in the php loop below. The php file loads but code stops working after " Fields With Red Asterisks * Are Required" Any Help would be great!

    function loadMulti() {
        var num_to_enter = $('#num_to_enter').val();
        $.ajax({
          type: "POST",
          url: "myphpfile.php",
          data: "num_to_enter=" + num_to_enter,
          success: function(){
        $("#multi").load('myphpfile.php')
          }
       });
        return false;
          }

and the php :

<?php
$num_to_enter = $_POST["num_to_enter"];
echo $num_to_enter;
$i=1;
?>

<form class="my_form" name="addReg" id="addReg" method="post" />


            <span class="red">Fields With Red Asterisks * Are Required</span>



<?php
    while($i <= $num_to_enter){
?>
    The html form here repeated by $num_to_enter
<?php
  $i++;
}
?>

3
  • 2
    You have invalid HTML, <form> is not self-closing. You never update $i in your PHP file, so it probably end up in a never ending loop. Commented Nov 20, 2013 at 23:09
  • You do know that you are missing a single quote at $("#multi").load(myphpfile.php')? should be $("#multi").load('myphpfile.php'). Commented Nov 20, 2013 at 23:31
  • hmm its in the org code ,sorry, typo here Commented Nov 21, 2013 at 1:22

3 Answers 3

1

For starters you can clean up your code a bit. See if this helps (tested and its working)

JS File

function loadMulti () 
{
    var num_to_enter = 2;//$('#num_to_enter').val();
    $.ajax({
        type: "POST",
        url: "temp.php",
        data: "num_to_enter=" + num_to_enter,
    }).done (function (data){  //success is deprecated
        $("#test").html (data);
    });
    return false;
 }

$(document).ready (function (){
    loadMulti ();
});

Or maybe you want a js post??

function loadMulti () 
{
    var num_to_enter = 2;//$('#num_to_enter').val();

    $ ("#check").on ("click", function (){
        $.ajax({
            type: "POST",
            url: "temp.php",
            data: "num_to_enter=" + num_to_enter,
        }).done (function (data){  //success is deprecated
            $("#test").html (data);
        });
    });
    return false;
 }

$(document).ready (function (){
    loadMulti ();
});

PHP File

<?php
  $num_to_enter = $_POST["num_to_enter"];
  $string = "";
  echo $num_to_enter;
  $i=1;

  while ($i <= $num_to_enter)
  {
        $string .= "The html form here repeated by {$num_to_enter}<br/>";
        $i++;
  }
?>


  <span class="red">Fields With Red Asterisks * Are Required</span>
  <?php echo $string; ?>

PHP File that makes the call.

<!doctype html>
 <html>
  <head>
   <title></title>
   <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
   <script src='test.js'></script>
  </head>

  <body>
    <div id="test">test</div>
  </body>   
 </html>

or with the post

<!doctype html>
 <html>
  <head>
   <title></title>
   <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
   <script src='test.js'></script>
  </head>


  <body>
    <div id="test">results will show here.</div>
    <form class="my_form" name="addReg" id="addReg" method="post" />
        <input id="check" type="button" name="sendpost" value="Get Data">
    </form>
  </body>   
 </html>

EDIT: Added the php file that makes the call, I changed the .load () to .html () with its respected selector.

Also I am not sure if you wanted the message to print out more then once, so if you need it printed that way just change $string to an array.

Sign up to request clarification or add additional context in comments.

10 Comments

xlordt I changed the code to your suggestion and now the php file does not load at all???? I even remove the comment out from the$("#multi").load(myphpfile.php');
ALso, to answer your question, the html form gets repeated by the number the user chooses - $num_to_enter
I should add that I added an alert and the variable num_to_enter is there and I have an echo of it in the php file but when the file loads it does not echo it . if I add a die statement and go to the php file direct with out ajax it does pass and forms print. it seems that just the php is not working when loaded through ajax. any other thoughts?????
did you check it using firebug?
I don't know how to do that, sorry!
|
1

Your PHP script 'works' fine, but you end up in a while loop that has no end because you never update $i. You need to increment it in the while loop:

<?php
    $num_to_enter = $_POST["num_to_enter"];
    echo $num_to_enter;
    $i = 1; 
?>

<form class="my_form" name="addReg" id="addReg" method="post" />

<span class="red">Fields With Red Asterisks * Are Required</span>



<?php
    while ($i <= $num_to_enter) { 

       ?>The html form here repeated by $num_to_enter <?php

       // You need to increment this so the while loop stops when $i hits 
       // the same amount as $num_to_enter.
       $i++; 
    }
?>

Comments

0

Change this:

data: "num_to_enter=" + num_to_enter,

to this:

data: {num_to_enter: num_to_enter},

$.ajax() expects an object, not a string. The docs do say that it can accept a string, but you have to serialize it yourself in that scenario; it's easier just to let jQuery deal with that.

Regarding the PHP: make sure you increment $i in your while loop, or it will never end, as @putvande pointed out, and make sure you include a closing </form> tag.

Finally, change this: $num_to_enter = $_POST["num_to_enter"]; to this: $num_to_enter = intval($_POST["num_to_enter"]); to force PHP to treat it as an integer.

4 Comments

That;s not true, it can be both a string or an object.
See my edit. It appears jQuery is prone to handling a string there correctly, as also demonstrated in this question.
I tested the data: "num_to_enter=" + num_to_enter just now, it works fine.
I changed data: "num_to_enter=" + num_to_enter, to this: data: {num_to_enter: num_to_enter}, and made sure form tag is closed edited above but still get same result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.