0

I wrote a piece of code, when the user click on submit button it send a string to PHP and then my code will run a Mysql query (based on the submitted string) and then using file_put_content it will upload the mysqli_fetch_array result to the file. All I want to do is without refreshing the page it submit the value to php form and run the code then show <a href="export.csv">Download From Here</a> to the user. How should I do that using javascript or jQuery ?

  if(@$_POST['submit']) {
         if (@$_POST['export']) {
             $form = $_POST['export'];
             echo $form;
             $con1 = mysqli_connect("localhost", "root", "", "test_pr");
             $sql2 = "SELECT email FROM `my_data` WHERE email LIKE '%$form%'";
             $result2 = mysqli_query($con1, $sql2);


             $rows = array();
             while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {
                 $rows[] = $row['email'] . PHP_EOL;
             }


             $nn = implode("", $rows);

             var_dump($rows);
             echo $nn . PHP_EOL;


             $file = fopen("export.csv", "w");


             file_put_contents("export.csv", $nn);


             fclose($file);
         }
        }


    ?>

    <html>

    <form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method=post>
        <input name="export" type="text" value="example"  /> Export Address<br/>
        <input name="submit" type="submit" value="submit"  />
        <a href="export.csv">Download From Here</a>
    </form>
    </html>
mr.shootz
  • 1
  • 2

1 Answers1

0

Assuming you know how to include jquery, you would first bind a submit handler to the submit button, (I've added an id to make it easier) and prevent the default submit action. Then add an AJAX post request to the handler. This will post to your php file. Have that file echo out your link, then have the ajax callback function append it to the desired element. Something like this:

<form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" id="form1" //Add an id to handle with >
    <input name="export" type="text" value="example"  /> Export Address<br/>
    <input name="submit" type="submit" value="submit"  />
    <a href="export.csv">Download From Here</a>
</form>
<script>
    $("#form1").submit(function (event) {
        event.preventDefault();
        $.post("//path of your php file here",{inputText: $("input[type='text']")},function (returnedString) {
            $("#whereToPutReturnedString").append(returnedString);
        });
    });
</script>

Also, if you want to just show the link when the button is clicked, do the following:

 <script>
     $("input[type='submit']").submit(function () {
         $("#idOfElementToPlaceLink").append("<a href="yourlink">Your anchor text</a>");
     });
 </script>

or you could just have it hidden with css or jquery and do $("#theId").show();

If you need more help, just holler!

Joshua
  • 17
  • 1
  • 6
  • Thanks but the second code is not working.... didn't tested the first part yet until I find-out how to include id. do I need to include any files to make the script running ? my knowledge is zero about JavaScript and Ajax... – mr.shootz Jul 11 '15 at 15:08
  • The second part will not work until you include the jquery library, have you done that? Otherwise it should work, just as long as you replace "#idOfElementToPlaceLink" with the id of the element you want to place the anchor in, and href="yourlink" with the file path. If you want to learn more about the .append() method and how it works, check out the documentation: http://api.jquery.com/append/ – Joshua Jul 13 '15 at 18:12