douzhi7754 2015-07-11 13:10
浏览 37

当用户使用html提交数据时,如何运行PHP代码

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>
  • 写回答

1条回答 默认 最新

  • douyan8413 2015-07-11 13:31
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看