skip to Main Content

Basically my shirt e-commerce website won’t redirect to my html website. Whenever I try to click on the virtual try on button next to the shirt, it doesn’t redirect me to the page instead it just loads and refreshes the current page. Any tips?
Here is my code:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="get">
    <input type="hidden" name="product_id" value="124">
    <input type="submit" value="Virtual Try ON" name="form_virtual_try_on">
</form>

<?php
$product_id = $_GET['product_id'] ?? '';

switch ($product_id) {
  case '124':
    $text = 'Virtual Try On';
    $link = 'vton_ls.html';
    break;
  default:
    $text = '';
    $link = '';
    break;
}

if (isset($_GET['form_virtual_try_on'])) {
  $product_id = $_GET['product_id'];
  if ($product_id == '124') {
    header('Location: vton_ls.html');
    exit;
  } else {
    echo "Invalid product ID";
  }
}
?>

<div class="share">
    <?php echo LANG_VALUE_58; ?> <br>
    <div class="sharethis-inline-share-buttons"></div>
</div>

I tried GET, switch case, and redoing all my codes from scratch but it doesn’t seem to work.

3

Answers


  1. You already added the header and pls add anchor tag too
    header (‘Location: vton_ls.html’);
    you’re trying to link from an HTML page to a PHP page, you can use an anchor tag like this:

    <a href="vton_ls.php">Virtual Try-On</a>
    
    Login or Signup to reply.
  2. By simply adding little snippets of code rather than the actual full page it is potentially easy to misinterpret what you are trying to do and why things are going awry so if the following is not correct I apologise.

    Given a testing target page (vton_ls.html) and an interpretation of the above

    <?php
        session_start();
        
        if( !defined('LANG_VALUE_58') )define('LANG_VALUE_58','LANG_VALUE_58......... ok');
        
        
        if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['product_id'] ) ){
            
            switch( intval( $_GET['product_id'] ) ){
                case 124:
                    $text = 'Virtual Try On';
                    $link = 'vton_ls.html';         
                break;
                default:
                    $text = '';
                    $link = '';
                break;
            }
            
            exit( header( sprintf('Location: %s?product_id=%s&text=%s', $link, $_GET['product_id'], $text ) ) );
        }   
    ?>
    <!DOCTYPE html>
    <html lang='en'>
        <head>
            <meta charset='utf-8' />
            <title></title>
        </head>
        <body>
        
            <form method='get'>
                <input type='hidden' name='product_id' value='124' />
                <input type='submit' />
            </form>
            
            <div class='share'>
                <?php echo LANG_VALUE_58; ?> <br>
                <div class='sharethis-inline-share-buttons'></div>
            </div>
        </body>
    </html>
    

    By clicking the button the form submits to the same page where the GET request is processed to build the new url that redirects to your html website

    Login or Signup to reply.
  3. If you fail to complete this, you’ll probably see an error message that tells you “headers are already sent.” The error might not be a massive one — the header function is so finicky that a single white space can prompt this error.

    <?php
    

    header(‘Location: http://www.example.com/‘);

    exit;

    ?>

    <head>
    
        <title>Example</title>
    
    </head>
    
    <body>
    
        <p>Hello, world!</p>
    
    </body>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search