Jump to content

Dynamically accessing html value within the same file


Go to solution Solved by kicken,

Recommended Posts

Hello everyone,
I am making a web app and I need the user to be able to enter a code and then the php will search for the column that the code is in and then output the content of said column. The issue is that I can't find a way to pass the value from the html to the php. With the <form> tag, it throws this error:

Quote

Warning: Undefined array key "sid" in /opt/lampp/htdocs/sendrata_sano/read.php on line 9

I also tried with cookies. They kind of work, the issue is that the page has to be refreshed first. Here is my code (sorry in advance for the mesh you are about to see)
 

<!DOCTYPE html>
<?php
  session_start(); //currently using this to test the rest of the app
  $SID = $_POST["sid"];
  echo $SID;
  $values_arr = array();
  //database connection
  $host = 'localhost';
  $dbname = "test";
  $username = "root";
  $password = "";
  // Create connection
  $conn = mysqli_connect(hostname: $host, username: $username, password: $password, database: $dbname);
  // Check connection
  if (mysqli_connect_errno()) {
    die("Connection failed: " . mysqli_connect_errno());
  }
  $sql = "SHOW COLUMNS FROM test";
  $result = mysqli_query($conn,$sql);
  for($o=0; $o<$row = mysqli_fetch_array($result); $o++){
      //if the column is the LID skip
      //echo $row['Field'];
      if($row['Field']==='LID'){}
      else{
      $column = $row['Field'];
      }
      if($o>0){
          $repeat[$o-1] = $column;
      }    
  }
  
  $sql = "SELECT * FROM `test` WHERE LID = $_SESSION[id]";//I currently use this to test the rest of the app
  $result = mysqli_query($conn, $sql); // First parameter is just return of "mysqli_connect()" function
  while ($row = mysqli_fetch_assoc($result)) { // Important line !!! Check summary get row on array ..
    $i = 0;
      foreach ($row as $field => $value) { // I you want you can right this line like this: foreach($row as $value) {
        //get every value in an index 
        $values_arr[$i] = $value;
        $i++;

          //echo $value; // I just did not use "htmlspecialchars()" function. 
      }
  }

  //make the array into a string
  $values_str = implode($values_arr);
  //turn that string back to an array
  $trimmed = str_split($values_str);
  //for the size of that array
  for($j=0; $j<sizeof($trimmed); $j++){ 
    //if there is " replace it with space
    if($trimmed[$j]=='"'){
        $trimmed[$j]=' ';
    }   
}
//turn that array back to a string
$new_str = implode($trimmed);
?>
<html>
  <head>
    <title></title>
  </head>
  <body>    
    <div id="demo" value="<?php echo "$new_str" ?>"></div>
    <div action = "read.php" id="form">
        <form method="post">
            <label id="sid_label" name ="sid"></label>
            <button id="button_clicker" onclick="get_sid(), show_message(); return false">READ</button>
        </form>
    </div>
    <div id="txt"></div>
  </body>
  <script>
    //check fot the sid (replace with the raspberry pi code)
    function get_sid(){
      let text = prompt("SID");
      let lbl = document.getElementById("sid_label");
      let txt = document.createTextNode(text);
      lbl.appendChild(txt)     
    }
    //show the selected row after clicking the button
    function show_message() {
      //get the value of the demo
      let id_param = document.getElementById("demo").getAttribute('value');
      //id of button
      let button_click = document.getElementById("txt");
      //create p element
      let text = document.createElement("p")
      //create text
      text_content = document.createTextNode(id_param);
      //put the text as a child of the p element
      text.appendChild(text_content);
      //put the p element as a child of the butto_click element
      button_click.appendChild(text);
    }
  </script>
</html>

Any solutions to this problem?

Any particular reason you aren't doing a normal form? Like

<form method="post">
  <label>SID: <input type="text" name="sid"></label>
  <button type="submit">Submit</button>
</form>

Because what you're doing is... I don't know? You've got bits and pieces of multiple paradigms going on in here, and none of them are going to work like that.

10 hours ago, requinix said:

Any particular reason you aren't doing a normal form? Like

<form method="post">
  <label>SID: <input type="text" name="sid"></label>
  <button type="submit">Submit</button>
</form>

Because what you're doing is... I don't know? You've got bits and pieces of multiple paradigms going on in here, and none of them are going to work like that.

I think i am just braindead and didn't notice that my html code is wrong. I 'll update you with the result of the fixed code

10 hours ago, requinix said:

Any particular reason you aren't doing a normal form? Like

<form method="post">
  <label>SID: <input type="text" name="sid"></label>
  <button type="submit">Submit</button>
</form>

Because what you're doing is... I don't know? You've got bits and pieces of multiple paradigms going on in here, and none of them are going to work like that.

    <div id="form">
        <form action = "read.php" method="post">
            <label id="sid_label" name ="sid"></label>
            <button id="button_clicker" type="submit" onclick="get_sid(), show_message()">READ</button>
        </form>
    </div>

It still doesn't work. Also, I need to get the sid with javascript, not from an input text. I am planning to automatically give an sid with a raspberry pi in the future, so I don't think that an input text will be a good start to achieve this

Edited by PaulosK
6 minutes ago, requinix said:

Forms only submit the data in their fields. Your form has no data in it at all.

If you don't need anything else but the SID then forget the form entirely and go with AJAX instead.

can i keep the code for the rest of the app the same and just rewrite this page with ajax, or how does it work? I haven't used ajax before, so I know nothing about it

  • Solution
4 hours ago, PaulosK said:

Also, I need to get the sid with javascript, not from an input text.

Why?  Your current JS code essentially just gets the SID using an input text field, just in a prompt() window instead of the page.  There's really no effective difference.  If this is just some placeholder for whatever you want to do with your RPi, maybe expanding on that would be good.  The only way I imagine grabbing a value with JS from a RPi would be via an AJAX request.

All in all, I'm confused and don't really understand what you're trying to do or why?

 

You say

15 hours ago, PaulosK said:

I need the user to be able to enter a code and then the php will search for the column that the code is in and then output the content of said column

As mentioned, the typical way to accomplish that would just be a simple html form without any JS complexity behind it.  The warning you mentioned is probably from trying to run the form processing code without having submitted the form.  If you're using the same page to display and process the form, your form processing code needs to only run when the form is submitted, not during the initial page load.  This is typically done with something like:

if ($_SERVER['REQUEST_METHOD'] === 'POST'){
  //Form processing code here
}

 

3 hours ago, kicken said:

Why?  Your current JS code essentially just gets the SID using an input text field, just in a prompt() window instead of the page.  There's really no effective difference.  If this is just some placeholder for whatever you want to do with your RPi, maybe expanding on that would be good.  The only way I imagine grabbing a value with JS from a RPi would be via an AJAX request.

All in all, I'm confused and don't really understand what you're trying to do or why?

 

You say

As mentioned, the typical way to accomplish that would just be a simple html form without any JS complexity behind it.  The warning you mentioned is probably from trying to run the form processing code without having submitted the form.  If you're using the same page to display and process the form, your form processing code needs to only run when the form is submitted, not during the initial page load.  This is typically done with something like:

if ($_SERVER['REQUEST_METHOD'] === 'POST'){
  //Form processing code here
}

 

i don't know how to pass send the message from the rpi to the app, but I haven't got to that stage yet, so it will be something I 'll do in the future. I think that it will need something with javascript thought, thus, I am making this function as a placeholder. The general idea of the app will be to read an nfc card, find its values from a db using the serial number of the nfc car (SID) and then display the values found on the db or write values to the db on the row with the specific SID. I 'll try what you said and i 'll update with the results.

5 hours ago, kicken said:

Why?  Your current JS code essentially just gets the SID using an input text field, just in a prompt() window instead of the page.  There's really no effective difference.  If this is just some placeholder for whatever you want to do with your RPi, maybe expanding on that would be good.  The only way I imagine grabbing a value with JS from a RPi would be via an AJAX request.

All in all, I'm confused and don't really understand what you're trying to do or why?

 

You say

As mentioned, the typical way to accomplish that would just be a simple html form without any JS complexity behind it.  The warning you mentioned is probably from trying to run the form processing code without having submitted the form.  If you're using the same page to display and process the form, your form processing code needs to only run when the form is submitted, not during the initial page load.  This is typically done with something like:

if ($_SERVER['REQUEST_METHOD'] === 'POST'){
  //Form processing code here
}

 

I tried what you propose and it worked. I also changed the form in html to this:
I also use php to show the outcome instead of js and only using js to get the SID

<form action = "read.php" method="post">

<input id="sid_label" name="sid" readonly="readonly">

<button id="button_clicker" type="submit" onclick="get_sid()">READ</button>

</form>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.