Home > OS >  Posting a variable in a php file
Posting a variable in a php file

Time:01-05

i've created 3 files. The main is html with javascript, the second one is based on php only and the third one uses html and inline php forms.

In main file, i've written the below script:

    $(document).on('click', '.print_fumes_card', function(){
        var user_id1 = $(this).attr("id");
        alert(user_id1);
        $.ajax({
            url:"print/print_fumes_card.php",
            method:"POST",
            data:{user_id1:user_id1},
            success:function(data)
            {
                window.open ("print/print_fumes_card.php","_blank");
            }
        })
    });

The above script is enabled, only when i press the button '.print_fumes_card' in second php file. The problem is that var user_id1 doesn't pass to third file (mix of html and php) "print_fumes_card.php" and i can't undertand why.

I need that variable in 3rd file to, execute some select queries.

The structure of thrid file is the following:

<!DOCTYPE html>
<?php
    require 'conn.php';
    echo $_POST["user_id1"];
?>
<html lang="en">
    <head>
        <style> 
        .table {
            width: 100%;
            margin-bottom: 20px;
        }   
        
        .table-striped tbody > tr:nth-child(odd) > td,
        .table-striped tbody > tr:nth-child(odd) > th {
            background-color: #f9f9f9;
        }
        
        @media print{
            #print {
                display:none;
            }
        }
        @media print {
            #PrintButton {
                display: none;
            }
        }
        
        @page {
            size: auto;   /* auto is the initial value */
            margin: 10;  /* this affects the margin in the printer settings */
        }
    </style>
    <link rel="stylesheet" href="style4.css" media="all" />
    </head>
<body>
    <header >
        <div id="logo">
            <img src="logo1.png">
        </div>
        <div id="company">
            <h2 >App</h2>
            <div>address</div>
            <div>phone</div>
            <div>email</div>
            <div><a href="web">web</a></div>
        </div>
        </div>
    </header>
    <main>
      <div id="details" >
        <div id="client">
          <div >Plate:</div>
          <div ><?php
                                require 'conn.php';
                                $result = $conn->query("SELECT * FROM vehicles WHERE plate=(SELECT plate FROM visits WHERE vid='".$_POST["user_id1"]."')");
                                $row = mysqli_fetch_array($result);
                                echo $row['plate'];
                                ?></div>
        </div>
....
....
..
.
.

The problem is that $_POST["user_id"] variable isn't recognized as in second file, which is only php. Is there any special technique to pass that variable in third file?

I've tried different combinations and i can't figure out what i'm missing.

CodePudding user response:

The two separate requests to print/print_fumes_card.php make little sense based on the information provided. The initial ajax post passes only user_id1 and does not appear to be attempting to make any change to the data. The second request does not pass the param at all. Just pass the param in the window.open call -

$(document).on('click', '.print_fumes_card', function(){
    var user_id1 = $(this).attr("id");
    alert(user_id1);
    window.open ('print/print_fumes_card.php?user_id1='   user_id1, '_blank');
});

And then -

<?php
    require 'conn.php';
    echo $_GET['user_id1'];
?>

The comment about understanding the SQL Injection vulnerabilities and using prepared statements still stands. At the very least you should force the type -

$user_id1 = (int) $_GET['user_id1'];
  • Related