0

I'm new to web programming and I have a problem with the website I'm trying to do : I want to send some data to another .php page when the user clicks a link (there are several links, each link is the title of an newspaper article and when the user clicks one of them, it sends the id of the article because the next page has to load the content of the article). But I must not use Javascript.

Here is my previous code :

<h2> Articles </h2>
<?php
$articlesSql = "SELECT id, title FROM Article A, ApproveArticle VA WHERE A.id = VA.article AND VA.state='published';";
$articlesQuery = pg_query($connect, $articlesSql);
while($articlesResult = pg_fetch_array($articlesQuery)) {
    echo "<form id=\"linkArticle\" action=\"article.php\">";
    echo "<input type=\"hidden\" name=\"article\" value=\"$articlesResult[id]\"> </form>";
    echo "<a href='#' onclick='document.getElementById(\"linkArticle\").submit()'> $articlesResult[title] </a> <br/>";
    }
?>

Is there a way to avoid using Javascript? If so, how do I do?

Thank you for your help in this matter and have a nice day.

Robert
  • 5,484
  • 1
  • 22
  • 35
Griffin_0
  • 47
  • 7

1 Answers1

0

There is no way to submit a form without using a submit button with HTML only.

You can however make your button look like a link. This will require some CSS. Have a look at this thread for inspiration.


A better alternative that also solves your problem would be to ditch the form completely.

Simply include the article id you want to transfer to your PHP script into the URL as a parameter:

echo "<a href=\"article.php?article=$articlesResult[id]\"> $articlesResult[title] </a> <br/>";

After the user clicked the link, you will be able to read the article id using $_GET['article'].

Community
  • 1
  • 1
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94