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++;
}
?>

Corn Hole LI
  • 63
  • 2
  • 11

3 Answers3

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++; 
    }
?>
putvande
  • 15,068
  • 3
  • 34
  • 50
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.

xlordt
  • 521
  • 4
  • 15
  • 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'); – Corn Hole LI Nov 21 '13 at 00:07
  • ALso, to answer your question, the html form gets repeated by the number the user chooses - $num_to_enter – Corn Hole LI Nov 21 '13 at 00:15
  • 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????? – Corn Hole LI Nov 21 '13 at 00:52
  • did you check it using firebug? – xlordt Nov 21 '13 at 01:01
  • I don't know how to do that, sorry! – Corn Hole LI Nov 21 '13 at 01:09
  • Edited answer, check it out. – xlordt Nov 21 '13 at 01:14
  • hmmm still not loading php file with .html() not sure what to do with the "php file that makes the call"?? – Corn Hole LI Nov 21 '13 at 01:31
  • The php file that makes the call is the file that will show the results that comes from jquery. Run that file on your server to see it. I have took the liberty to upload mines so you can see the results at http://latingeeks.net/temp/1.php – xlordt Nov 21 '13 at 01:34
  • Edited again, as I think what you are looking for is a post action... again check out the link I pasted for a review. – xlordt Nov 21 '13 at 01:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41670/discussion-between-corn-hole-li-and-xlordt) – Corn Hole LI Nov 21 '13 at 21:51
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.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • 1
    That;s not true, it can be both a string or an object. – putvande Nov 20 '13 at 23:14
  • See my edit. It appears jQuery is prone to handling a string there correctly, as also demonstrated in [this question](http://stackoverflow.com/questions/5046930/jquery-send-string-as-post-parameters). – elixenide Nov 20 '13 at 23:17
  • I tested the `data: "num_to_enter=" + num_to_enter` just now, it works fine. – putvande Nov 20 '13 at 23:18
  • 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 – Corn Hole LI Nov 20 '13 at 23:21