1

how can I redirect the user to another php page based on the button he is pressing? For example .. I want that once loaded the page, in it are generated buttons containing the "id" of the table taken from a database ... At the click of the button you are redirected to a page in which there are textbox with the fields belonging to the table id ..

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT idCantiere,nomeCantiere,codiceCommessa,indirizzoCantiere FROM Cantiere";
$result = $conn->query($sql);
echo'<h1> <font face="verdana" color="green">Quale Cantiere desideri Modificare?</font> </h1>';
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
		
		echo'<br><br><br>';
		echo'<a href="#" onClick=navigaButton(); class="myButton" alt="">' . $row["nomeCantiere"] . '</a>';
       // echo '<a href="#" class="rainbow-button" alt="' . $row["nomeCantiere"] . '"></a>';
		
		//''<input type="button" value="' . $row["nomeCantiere"] . '" />'
    }
	echo'<br><br><br>';
	echo '<a href="../pagineHtml/inserimento/inserimentoGenerale/inserimentoCantiere.php" class="myButton" alt="Nuovo Cantiere +">Nuovo Cantiere +</a>';
	
} else {
    echo "0 results";
}
$idCantierePerSelect = $_POST["idCantiere"];
global = $idCantierePerSelect;


function navigaButton()
{
  // FUNCTION That redirect to the page
};

$conn->close();
?>

So I have to pick up the "idCantiere" and I have to make sure that by clicking on the button on the page that opens me there are textBox with the data of the table of the "idCantiere"

2
  • the function navigaButton would need to be javascript and you would need to pass it the id somehow. Commented Oct 1, 2018 at 7:56
  • @RamRaider thanks for the reply, but how can I pass it on? Commented Oct 1, 2018 at 7:58

2 Answers 2

1

I think you are confusing the static html between dynamic server page.
1.PHP is responsible for fecthing data from database or server file system ,and send html tags to front end
2.the browser receives strings from php , and parse the strings to html elements ,finally starts to run javascript

If you want to redirect page.
In php header('Location: /your/path')
In javascript , window.location.href='/your/path'

Sign up to request clarification or add additional context in comments.

2 Comments

this is not the problem! Maybe I did not have to explain well .. I need a way to pass a variable like "idCantiere" in another php page
@NarcosZTK_10 generate javascript by PHP,like <?php $id="xx";echo "<script>alert({$id})</script>"?> .If you want to use the id in every php page , use $_SESSION to store it ,like a.php<?php session_start(); $_SESSION['id']='xx';?> b.php:<?php session_start(); echo $_SESSION['id']; ?>
0

Very quickly written and not tested - you could perhaps do like this. The function has to be javascript to interact with the client's actions ( ie: button press ) but the processing of the task is done by php. So, in the loop pass the ID of the record to the function as an inline argument and reload the same page with a new querystring. PHP will process the querystring, find the ID and then do a different database lookup to find the page that you want to redirect to.

<?php

    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "";


    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) die("Connection failed");



    /* 
        This section should be ignored when page loads normally
        but will be processed when the `navigaButton` is called
        and the url changes.
    */
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['action'],$_GET['id'] ) ){

        $sql='select NEWLOCATION from TABLE where ID=?';
        $stmt=$conn->prepare( $sql );
        if( $stmt ){
            $stmt->bind_param('i',$id);

            $id=filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT );
            $stmt->execute();

            $stmt->store_result();
            $stmt->bind_result( $url );
            $stmt->close();


            /* read the recordset and .... */
            exit( header( 'Location: '.$url) );
        }

    }



?>

<!doctype html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>.... </title>
        <script>
            function navigaButton(id){
                location.search='action=redirect&id='+id
            };
        </script>
    </head>
    <body>
        <?php
            $sql = "SELECT idCantiere,nomeCantiere,codiceCommessa,indirizzoCantiere FROM Cantiere";
            $result = $conn->query($sql);


            echo'<h1> <font face="verdana" color="green">Quale Cantiere desideri Modificare?</font> </h1>';

            if ($result->num_rows > 0) {

                while($row = $result->fetch_assoc()) {

                    echo'
                    <br><br><br>
                    <a href="#" onClick=navigaButton('.$row['idCantiere'].'); class="myButton" alt="">' . $row["nomeCantiere"] . '</a>
                    <a href="#" class="rainbow-button" alt="' . $row["nomeCantiere"] . '"></a>';

                }

                echo'<br><br><br>
                <a href="../pagineHtml/inserimento/inserimentoGenerale/inserimentoCantiere.php" class="myButton" alt="Nuovo Cantiere +">Nuovo Cantiere +</a>';

            } else {
                echo "0 results";
            }

            $idCantierePerSelect = $_POST["idCantiere"];

            /* below is incorrect */
            /*global = $idCantierePerSelect;*/


            $conn->close();
        ?>
    </body>
</html>

2 Comments

Thanks again for the answer, but I do not know if you understand me I would need a way to pass the variable "idCantiere" from one page to another .. let's say that this is the page "choose.php" and I click on the button 1 with "idCantiere = 1" this variable I have thought to put it Global in such a way that it is viewable from all the pages of the DB ".. In this case, I open a generic page "view.php" ... in which I will make a query on the database of the type "SELECT idCantiere, nomeCantiere etc .. FROM Cantiere WHERE idCantiere =" variable that I have taken from "choose.php" "
That is more or less what does happen ( or should ) with the alterations made above - it will result in a querystring being appended to your page url - similar to http://www.example.com/index.php?id=23&action=redirect - where idCantiere in this respect is 23 and referred to as id in the querystring. If this is not what you mean then I have failed to comprehend and perhaps you might explain further?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.