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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:41:24+00:00 2026-05-16T06:41:24+00:00

I’ve put together a little script that works fine in general, it just needs

  • 0

I’ve put together a little script that works fine in general, it just needs a minor fix and some Ajax code. Basically, it appends a couple of forms to each output line. Submission works properly, 2 POSTS and 1 GET. But I’d like it to happen without a page refresh, ergo the Ajax code. I’ve tried messing around with some but the multiple variables are making it a nightmare. Here’s the full code, first the main page:

<html>
<head>
<title></title>
</head>
<body>
<?php

$lines = file('file.txt');

foreach ($lines as $line) {
$field = explode("|", $line);

$name = $field[0];
$id = $field[4];

echo $name . '<br>';?>

<form method="post" action="submit-here.php?id=<?php echo $id;?>">

<select name="FORM1" size="3">

<option value="Value-1">Value-1</option>
<option value="Value-2">Value-2</option>
<option value="Value-3">Value-3</option></select>&nbsp;&nbsp;&nbsp;

<select name="FORM2" size="3">

<option value="Value-4">Value-4</option>
<option value="Value-5">Value-5</option>
<option value="Value-6">Value-6</option></select>

<br><br><input type="submit" value="submit" name="submit">

</form>

<?php
}
?>

</body>
</html>

file.txt is just your usual DSV flat file i.e. field1|field2|field3|field4\n

Which brings me to the required minor fix I mentioned, namely the line break at the end of each row generates a pair of “phantom” forms after every legitimate form pair, add more line breaks and get more ghost code, I tried using preg_replace and if…else to filter them out but without success.

submit-here.php:

$id = $_GET["id"];
$FORM1 = $_POST["FORM1"];
$FORM2 = $_POST["FORM2"];


$write = $id . '|' . $FORM1 . '|' . $FORM2 . "\n";


$fn = "file2.txt";
$fh = fopen($fn, 'a');
fwrite($fh, $write);
fclose($fh);
  • 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-16T06:41:25+00:00Added an answer on May 16, 2026 at 6:41 am

    Multiple Forms Part:

    This works for me:

    <html>
    <head>
    <title></title>
    </head>
    <body>    
    <?php
    $lines = file('test.txt');
    $count=0;
    foreach ($lines as $line) {
        $field = explode("|", $line);
        if (count($field) > 2) {
            //only print the form if there is real data
            print_form($field, $count);
            $count++;
        }
    }
    ?>
    </body>
    </html>
    
    <?php
    function print_form($field, $num) {
      $name = $field[0];
      $id = $field[4];
    
      echo $name . '<br>';?>
    
      <form method="post" action="submit-here.php">
    
      <input type="hidden" id="id_<?=$num?>" value="<?php echo $id;?>">
      <select id="sel1_<?=$num?>" name="FORM1" size="3">
    
      <option value="Value-1">Value-1</option>
      <option value="Value-2">Value-2</option>
      <option value="Value-3">Value-3</option></select>&nbsp;&nbsp;&nbsp;
    
      <select id="sel2_<?=$num?>" name="FORM2" size="3">
    
      <option value="Value-4">Value-4</option>
      <option value="Value-5">Value-5</option>
      <option value="Value-6">Value-6</option></select>
    
      <br><br>
      <input type="button" value="submit" name="submit" onClick="submitForm(<?=$num?>);">
    
      </form>
    
    <?php
    }
    

    AJAX Part

    In your html head, add the following script block:

    <script language="JavaScript" type="text/JavaScript"> 
    <!--
    function submitForm(num) {
        //issue AJAX request
        var data = "FORM1="+document.getElementById("sel1_"+num);
        data = data+"&FORM2="+docucument.getElementById("sel2_"+num);
        data = data+"&id="+document.getElementByID("id_"+num);
        var request = getXmlHttpObject();
        request.onreadystatechange = 
          function() { if(request.readyState == 4 && request.status == 200) 
                          alert("Form received."); };
        request.open("POST", "submit-here.php", true);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        request.send(data);
    }
    
    function getXmlHttpObject()
    {
        if (window.XMLHttpRequest)
        {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          return new XMLHttpRequest();
        }
        if (window.ActiveXObject)
        {
          // code for IE6, IE5
          return new ActiveXObject("Microsoft.XMLHTTP");
        }
        return null;
    }
    //-->
    </script> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.