Jump to content

Dynamic creating of "form buttons" and action


Temnis

Recommended Posts

Hello!

 

I am pretty new to php & mySQL and I have some questions regarding to it.

 

I have a function that reads out a mySQL database and shows the information in a HTML - table. It also creates buttons in some rows in a seperate column, depending on the value of the column named "ART". This part works already. Now the created buttons shall call the switchID() - function when clicked. But I don't have a clue how to do that. I marked the part in the code. Any advice is welcome!

 

Here is the code:

 

functions.php

// -----------------------------------------------------------------------function getTable($page, $type){// -----------------------------------------------------------------------	require '/var/www/con2mySQL.php';	$result = mysqli_query($con,"SELECT * FROM CONNECTION");		// Creating first row        echo     "<table border='1'><tr>	          <th>ID</th>		  <th>Zustand</th>		  <th>Art</th>		  <th>Device</th>		  <th>Schalter</th>		 </tr>";// Creating the other rows	while ($row = mysqli_fetch_array($result)) {											$buttonName = "button" . $row['ID'];		echo "<tr>";		echo "<td>" . $row['ID'] . "</td>";		echo "<td>" . $row['ZUSTAND'] . "</td>";		echo "<td>" . $row['ART'] . "</td>";		echo "<td>" . $row['DEVICE'] . "</td>";		echo "<td>";// Creating the button		if ($row['ART'] == "o"){			echo    "<form name='Buttons' action='" . $page . "." . $type . "' method='post'>				    <input type='submit' name='" . $buttonName . "' value='Zustand wechseln' />	                         </form>";		}// -----------------------------------------------------------------------	// Giving the button a function. This is the part I struggle and don't know how to solve.		echo	"<?php                          if(isset($_Post[" . $buttonName . "])){			     switchID(" . $row['ID'] . ",0,1);			 } 			 ?>";// -----------------------------------------------------------------------		echo "</td>";		echo "</tr>";			}	echo "</table>";	mysqli_close($con);								}

Here is also the index.php, where the getTable() - function is used.

<?php require_once('/var/www/functions.php'); ?><html><head> <title>Testing</title></head><body><form name="Buttons" action="index.php" method="post">   <input type="submit" name="GetTable" value="Datenbank auslesen" />   <input type="submit" name="Kill0to1" value="ID 1904 0 -> 1" />   <input type="submit" name="Kill1to0" value="ID 1904 1 -> 0" /></form><?php	if  (isset($_POST["GetTable"])) { 		getTable(index, php);    }	if  (isset($_POST["Kill0to1"])) { 		switchID(1904,0,1);    } 	if  (isset($_POST["Kill1to0"])) { 		switchID(1904,1,0);    }?></body></html>

PS. Sorry for my bad english. B)

Link to comment
Share on other sites

You're going about it the wrong way. A button click does not execute a PHP function. One option would be to have the button click call a Javascript function, which sends an ajax request to the server to execute the PHP code and get a response back from the PHP that it can then use to update the page.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...