Click here to Skip to main content
15,905,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to set Parameter in Html Page which is redirect from Php with Parameter as below

login.php
<?php

$servername = "localhost";
$database = "mydatabase";
$username = "myuser";
$password = "mypwd";

// Create connection

$conn = mysqli_connect($servername, $username, $password, $database);
  
    
    $sql = mysqli_query($conn,
    "SELECT * FROM myuser WHERE mobi='"
    . $_POST["mobi"] . "' AND
    pwd='" . $_POST["pwd"] . "'    ");

    $mobi=$_POST['mobi'];   
   
    $num = mysqli_num_rows($sql);
   
    if($num > 0) {
    header("Location:https://brandstuckers.com/index.html?mobi=" .$mobi);

    exit();
    
       }
    else
    echo "Invalid Credential";
    
    
    

?>


Index.php


<!Doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>

 
</head>
<body style="margin:0;padding:0">
	<div style="width:100%;" >
		<img  src="img/logo1.jpg" style="display:inline; width:170px; height:70px;" />

		<a href="logout.html" style="display:inline;float:right;padding-right:1%;text-decoration:none;font-size:15px;">LogOut</a>
		
			<a href="login.html" style="display:inline;float:right;padding-right:1%;text-decoration:none;font-size:15px;margin-bottom:0">Login</a>
			

		    <input type="hidden" id="nn" style="display-inline;float:right;padding-right:1%" name="mobil" value="<?php echo $mobi; ?>"/>
		    
		    <span id="nn123"></span>

		</div>

<nav class="navbar navbar-expand-lg navbar-light bg-light" style="margin-top:20px">
    <div class="container-fluid">
        <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbarCollapse">
            
        </button>
        <div class="collapse navbar-collapse" id="navbarCollapse">
            <div class="navbar-nav">
                <a href="#" style="color:black;" class="nav-item nav-link active">Home</a>
                <div class="nav-item dropdown">
                    <a href="#" style="color:black;" class="nav-link dropdown-toggle" data-bs-toggle="dropdown">Products</a>
                    <div class="dropdown-menu">
                        <a href="tshirt.html" style="color:black;" class="dropdown-item">T-Shirt</a>
                        <a href="#" style="color:black;" class="dropdown-item">Jeans</a>
                    </div>
                </div>
                <a href="#" class="nav-item nav-link disabled" style="color:black;" tabindex="-1">About Us</a>
                <a href="#" style="color:black;" class="nav-item nav-link disabled" tabindex="-1">Contact Us</a>
            </div>
        </div>
    </div>
</nav>


    
</div>



<script>
    document.getElementById( 'nn123' ).innerHTML = document.getElementById( 'nn' ).value;

</script>



</body>
</html>


I am not define variable in index.php but set in input hidden. So how to define $mobi variable in index.php page please suggest.

What I have tried:

I am browsing this issue from two days on Google but not success
Posted
Updated 6-Feb-23 1:02am
v2
Comments
Member 15627495 6-Feb-23 6:05am    
header("Location:https://brandstuckers.com/index.html?mobi=" .$mobi   , true);

1 solution

Firstly, while you are developing your code, use this at the top of each PHP page, this will return the errors thrown so you can easily identify where your code errors occur -
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>


Looking at your code, you have a no return data because of some operators missing at this line -
header("Location:https://brandstuckers.com/index.html?mobi=" .$mobi);
//This should look like the following -
header("Location:https://brandstuckers.com/index.html?mobi=" .$mobi. "");


If this does not help, let us know on which line the error occured and what error were returned.

Looking at your edited question, you are calling a re-direct to index.html, your code is however in index.php

I will then suggest that you rather store all the variables in a session and call it from there. You can do this easily enough, read up on sessions from THIS PHP manual.

In your login.php, right at the top as the first line of code, add the following -
if (session_status() == PHP_SESSION_NONE) {
	session_start();

	//Create a new session id after each login...
	session_regenerate_id(true);

	error_reporting(E_ALL);
    ini_set('display_errors', 1);
//Remember to comment the error checking out after development...
}
//Now add your values to your session -
$_SESSION['mobi'] = $mobi;


In your index.php page (maybe look at naming this page different as index pages is your landing or 1st page in your site, then followed by login or whichever other page) -
<input type="hidden" id="nn" style="display-inline;float:right;padding-right:1%" name="mobil" value="<?php echo $_SESSION['mobi']; ?>"/>


You can add as many variables to your sessions as you wish, just read up more on closing sessions, flushing variables and arrays etc.
 
Share this answer
 
v2
Comments
MAHESH WAGHELA 6-Feb-23 6:26am    
Warning: main(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/brandstu/public_html/index.php on line 27

Notice: Undefined variable: mobi in /home/brandstu/public_html/index.php on line 27
MAHESH WAGHELA 6-Feb-23 6:28am    
Error at index.php page vaiable not properly set at hidden in html
Andre Oosthuizen 6-Feb-23 6:45am    
It seems that your calls to variables is on different pages, edit your question with the code for page 1 and then code for page 2 to make sense of where which call is made.
MAHESH WAGHELA 6-Feb-23 7:04am    
check it I have changed it as per your suggestion
Andre Oosthuizen 6-Feb-23 7:37am    
Updated my solution with Session

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900