0

I am trying to place a variable in a url and submit the form automatically

I am doing a mysql select, and based on the results I need to do as explained above.

PHP

<?php
$stmt = $conn->prepare("SELECT prime_ID,
                        FROM School 
                        WHERE Date = (CURDATE() - INTERVAL 2 DAY)");

$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $SchoolId = $row["prime_ID"];


  header("Location:../SchoolList.php?school=" . $SchoolId . "&Submit=Submit");

}
?>

HTML of SchoolList.php

 $shoolid = $_GET['school'];
 <form method="POST" action="test.php">
        <input type="text" name="school" id="school" value="<?php echo $shoolid ?>">
        <br>
        <input type="Submit" name="Submit" id="Submit" value="Submit">
 </form>

The SELECT works but the form does not submit. The variable is also visible in the next page. Although the form redirect to SchoolList.php, the URL looks like this SchoolList.php?school=45&Submit=Submit

Why does it not submit SchoolList.php?

  • Where is `$shoolid` defined? You also shouldn't use `header` in the `while`, or if you are use and `exit` but that makes the loop pointless. – user3783243 Sep 20 '18 at 13:16
  • while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $SchoolId = $row["prime_ID"]; – Charles Tester Sep 20 '18 at 13:17
  • 2
    Your MySQL synatx ist broken due a "," – BlackNetworkBit Sep 20 '18 at 13:18
  • The select works. Just the submit part not working – Charles Tester Sep 20 '18 at 13:19
  • @CharlesTester Do you want to go schoollist.php and submit the form automatically? – akcoban Sep 20 '18 at 13:19
  • `SELECT prime_ID,` < no way that works. – Funk Forty Niner Sep 20 '18 at 13:19
  • how do you use the URL params? How do you know it's not submitting? – treyBake Sep 20 '18 at 13:20
  • @FunkFortyNiner. Typo. I simplified the select not to bore everyone with code that already works. The only issue is the submit – Charles Tester Sep 20 '18 at 13:21
  • 1
    @user3783243 I made a new edit to include the code I left out. Let's see if gets accepted. – Sam Sep 20 '18 at 13:21
  • you would be surprised as to what people will post and we take posted code seriously. If you're going to simplify something, please don't include syntax errors, some may post an answer based on it, or the question being closed. – Funk Forty Niner Sep 20 '18 at 13:22
  • @ThisGuyHasTwoThumbs it gets stuck on SchoolList.php. SchoolList.php does not what it is suppose to – Charles Tester Sep 20 '18 at 13:22
  • I think you should enable error reporting and check for errors on the query. – Funk Forty Niner Sep 20 '18 at 13:23
  • `` where is that variable assigned? Again; error reporting said what? That's wrong for 2 reasons, should that be the actual variable name. – Funk Forty Niner Sep 20 '18 at 13:29
  • I disagree with the duplicate. It is no where close the the same question – Charles Tester Sep 20 '18 at 13:39
  • Could you clarify a few things here - assuming you have 3 scripts, one to get records to send from the School table, one with a form and then test.php - if you're looking to send a POST request to test.php with values from your School table, the way to do this is by using a CURL request (read https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code ) -the header() redirect you're doing will actually only execute the _last_ request, not each of them. If you replace the header() with a curl request to test.php it should work if this is what you're trying to do? – simandsim Sep 20 '18 at 13:42
  • @simandsim. I see that the comment part of the answer mention "sends a HTTP POST to a remote site", do I still need to include the code if test.php is on my server? Will it work if I have 10 results each needs to go to test.php with its unique variable? – Charles Tester Sep 20 '18 at 13:58
  • Yes - you can still use your local server but you need to include the domain name for it too so something like `http://localhost/test.php` will do it, or the public domain name if you have one. For each record you have, you'll want to create a curl request, then it won't matter if you have 1 or 1,000 records, it will post each one individually and the curl request will wait until each is done before moving onto the next. – simandsim Sep 20 '18 at 14:04

1 Answers1

-1

You can use js with php. Add that code block to end of the body.

<?php
    if (isset($_GET['Submit']))
{?>

<script type="text/javascript">
    document.getElementById('formId').submit();
</script>

<?php } ?>

and your form (id added to form):

<form method="POST" id="formId" action="test.php">
        <input type="text" name="school" id="school" value="<?php echo $shoolid ?>">
        <br>
        <input type="Submit" name="Submit" id="Submit" value="Submit">
 </form>
akcoban
  • 953
  • 7
  • 14