0

Well, here is my question. So, I have a table, displaying some data using PHP and SQL like this:

Then, as I showed in that pic, I want to click that button, get the ID value and with that, do another SQL sentence and show another table in another PHP file that I have.

My problem is, that as its mentioned here you cant really use AJAX to send it and then redirect. And im literally new to AJAX and JavaScript...

Of course, i know other ways like <form> and its submit element, but I already have one <form> in there, used for displaying the rows of the table, and receive new data input. So if I use form, I would have to nest them, which doesn't work.

I was trying this:

Table file (Where i have the id that i want to send)

<script>
      function openCateg(id) {
        $.ajax({
            data: {variable: id},
            url: 'categoriasSentenciasAJAX.php',
            type: "POST",
            success: function(data) {
              alert(data);
            }
        });
        //window.location = "categoriasSentenciasAJAX.php";
      }
    </script>

categoriasSentenciasAJAX.php (Where i wanna receive the data)

 $q = $_POST['variable'];

I also tried the GET method, just calling:

window.location = "categoriasSentenciasAJAX.php?variable=" + id;

and

 $q = $_GET['variable'];

Actually, it did work, but when i reload the site, it doesn't cause the variable becomes null.

My Table (generated from a SQL sentence in PHP, a basic SELECT

<form method="post" onsubmit="return confirm('¿Desea actualizar los datos?')">
        <table class="w3-table w3-striped w3-bordered" id ="table" width="70%" border="1px">
          <thead>
            <tr class="w3-theme">
                <th>IDEmpresa</th>
                <th>Usuario</th>
                <th>Nombre*</th>
                <th>Direccion*</th>
                <th>Ambito*</th>
                <th>Color*</th>
                <th>Categoria</th>
            </tr>
          </thead>

          <tbody>
            <?php
            foreach ($row as $key => $row) {
                ?>
                <tr class="w3-white">
                    <td><input type="text" class="w3-input" readonly="readonly" name="idE[]" value="<?php echo $row["idEmpresa"]?>" required/></td>
                    <td><input type="text" class="w3-input" readonly="readonly" name="idU[]" value="<?php echo $row["Usuario"] ?>" required /></td>
                    <td><input type="text" class="w3-input" name="nombre[]" value="<?php echo $row["Nombre"] ?>" required /></td>
                    <td><input type="text" class="w3-input" name="direccion[]" value="<?php echo $row["Direccion"] ?>" required /></td>
                    <td><input type="text" class="w3-input" name="ambito[]" value="<?php echo $row["Ambito"] ?>" required /></td>
                    <td><input type="color" class="w3-input-color" name="color[]" value="<?php echo rgb2hex2rgb($row["Color"]) ?>" /></td>
                    <td >
                      <button id="cat" type="button" name="button[]" class="w3-btn w3-dark-grey w3-hover-light-grey" onclick="openCateg(<?php echo $row["idEmpresa"]?>);">Categorias</button>
                    </td>
                </tr>
                <?php
            }
            ?>
          </tbody>
        </table>
      </div>
    </div>
        <br>
        <div class="w3-center">
          <div class="w3-padding-32">
            <input type="submit" class="w3-btn w3-xlarge w3-dark-grey w3-hover-light-grey" value="Actualizar Empresas" name="update">
          </div>
        </div>
    </form>

So, in summary, i just wanna click that button showed in the pic, get the id and use it to send it to another file, where i will call a SQL sentence to show another table. I would like to redirect to the new php file if possible too.

Just want to send the variable to the other php file without dissapearing when i press the refresh button of the browser.

Excuse my poor English, please...

Thanks in advance!

4 Answers4

0

If your value becomes null when you refresh then try setting the value which you are passing as default value.

document.getElementById("button_id").defaultValue = id;
Anitha s
  • 43
  • 1
  • 9
0

Why you want it to be redirected?post your value and get the response,on success you can do everything you want.

function OpenCateg(id)
{
variable = id;
$.ajax({
type: "POST",
url: "categoriasSentenciasAJAX.php?variable="+id,
success: function(data){
window.location = "categoriasSentenciasAJAX.php";

},
error: function(data){ alert('Error occured'); }});}
Aqil
  • 104
  • 10
  • I want to redirect it when the variable is posted, cause that variable is needed in my other php file to display a new table. Its a button that leads me to all the categories that a company can have. So lets say idEmpresa = 1, then i need that 1 to do a SQL Select to display the categories. – Axel Fernández May 31 '18 at 10:49
  • why use ajax then? – samezedi May 31 '18 at 11:26
  • I don't you need to use ajax at all, just redirect to that page and perform your queries, with the ID still intact – samezedi May 31 '18 at 11:29
  • I thought it was the best aproach, cause i can't really use another form or something like that. If i do it passsing the variable by url, it crashes when refreshing. – Axel Fernández May 31 '18 at 11:31
  • I'm really mixed,and can't see the pic coz my VPN not working right now,you can open session for that ID too – Aqil May 31 '18 at 11:36
  • that means the problem is in the other php page, don't you think? – samezedi May 31 '18 at 11:36
0

try this:

 function openCateg(id){
     window.location.assign('categoriasSentenciasAJAX.php?id='+id);
 }
samezedi
  • 621
  • 5
  • 15
0

Since you still redirect to the same categoriasSentenciasAJAX.php page, then you don't need ajax. Just use hyper-link. Change this:

<td >
    <button id="cat" type="button" name="button[]" class="w3-btn w3-dark-grey w3-hover-light-grey" onclick="openCateg(<?php echo $row["idEmpresa"]?>);">Categorias</button>
</td>

To this:

<td>
    <a href="categoriasSentenciasAJAX.php?variable=<?php echo $row["idEmpresa"] ?>" class="w3-btn w3-dark-grey w3-hover-light-grey">Categorias</a>
</td>

Use $_GET array:

$q = $_GET['variable'];

Also you'll probably have to adjust CSS styles of element so it would look as your button now, but I think that shouldn't be much problem =)

If page at URL categoriasSentenciasAJAX.php with variable param crashes on reload, then problem should be in this php file. We need it's code here and error message in order to understand what's going on.

Djengobarm
  • 398
  • 3
  • 7
  • Oh, its not the same page, the table is in a table.php file, and the other one, where i want to send the variable, is the categoriasSentenciasAJAX.php, but i will try this! – Axel Fernández Jun 01 '18 at 06:25
  • Yes, I think I understood correctly, but didn't explain myself enough. I mean that you do ajax request to *categoriasSentenciasAJAX.php* and then after you get responce you redirect to the same *categoriasSentenciasAJAX.php*. Or am I wrong? If I'm right, then skip ajax call and just redirect to *categoriasSentenciasAJAX.php?variable=ID_HERE* . You may need to adjust code in *categoriasSentenciasAJAX.php* so it handles it like regular http get request, not ajax. – Djengobarm Jun 01 '18 at 10:08
  • Oh, i see! You are right then! I will try that, though, i tried similar things and doing it by URL variable makes it dissapear when i refresh, is so strange... Im going to test this tho! Thanks! – Axel Fernández Jun 01 '18 at 10:09
  • Using this method its better cause I actually can display the data that i wanted, but the refreshing error is still here, and i dont know how to fix this... – Axel Fernández Jun 04 '18 at 06:34
  • I think that problem is in categoriasSentenciasAJAX.php code. If you post it here, then probably I'll be able to help. – Djengobarm Jun 04 '18 at 09:50