Form Submit button doesnt work with PHP loop - php

I have this code
<tbody>
<form action="dsf.php" method="post">
<?PHP if(mysql_num_rows($leerdb) > 0) {while ($rs = mysql_fetch_row($leerdb)) {?>
<tr>
<td><input name="idecod[]" type="checkbox" value="<?php echo $rs[0]; ?>" /></td>
<td><?php echo $rs[0]; ?></td>
<td><?php echo $rs[1]; ?></td>
<td><?php echo $rs[2]; ?></td>
<td><?php echo $rs[3]; ?></td>
<td><?php echo $rs[4]; ?></td>
<td><?php echo $rs[5]; ?></td>
<td>BsF. <?php echo $rs[6]; ?></td>
</tr>
<?PHP }}?>
<input class="enviar" type="submit" name="enviar" id="enviar" value="Editar Asesor" />
</form>
</tbody>
I do not know why the submit button doesnt work. When I click on it, nothing happend.
It seems that something in the php loop is making the mess.. But as I know, PHP goes first than HTML, so, when FORM HTML comes, PHP code was already executed.
Where is my error.??
Thanks in advance.
Roberto

You are generating invalid HTML.
You can't have a form wrapped around table rows without wrapping it around the entire table.
You can't have a submit button placed between table rows.
The browser you are using is likely trying to recover from the error in such a way that the form is moved somewhere where it is allowed, but where it doesn't contain any of the controls.
NB: Different browsers recover from having forms in inappropriate parts of tables in different ways.
Use a validator, and write real HTML.

instead of opening the brackets for the while and if, use the php short tags. Example:
<?php if (condition): ?>
output your html here
<?php endif; ?>
Same thing goes for while. read about it here: www.php.net/manual/en/control-structures.while.php

Related

How to make a button inside a table execute a mysql query

I have a simple html table that displays data from a mysql table containing a list of available tournaments.
<table>
<tr>
<th>Tournament Name</th>
<th>Platform</th>
<th>Date</th>
<th>Venue</th>
<th>Judge</th>
<th>Description</th>
<th>Limit</th>
<th>Register</th>
</tr>
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['nTournament'];?></td>
<td><?php echo $row['pTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['vTournament'];?></td>
<td><?php echo $row['jTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['lTournament']; ?></td>
<td><form method="post" action="tournamentlist.php"><button type="submit" name="btn_register"></button></form></td>
</tr>
<?php endwhile; ?>
</table>
What I want to do is that once the current user clicks that button, the button executes a query which takes the current user's id and the tournament id of the selected row to add them to another mysql table. Something like a tournament inscription.
The query/function I want to run is:
if (isset($_POST['btn_register'])) {
singup();
}
function singup() {
global $db;
$idUser = $_POST['idUser'];
$idTournament= $_POST['idTournament'];
$query = "INSERT INTO registeredCompetitor(idUser, idTournament) VALUES ('$idUser', '$idTournament')";
mysqli_query($db, $query);
header('location: tournamentlist.php');
}
The query works just fine on it's own but I don't know if the function itself works because of the button.
Is it possible to do such thing without the use of a form? If not, What other ways are there to do something like this?
EDIT1: Button now is ready but nor the function or the query executes once it's clicked.
You can create a form on each row that has 2 hidden fields in it. Not sure what your DB fields are called though so you'll have to change user_id and tournament_id to something appropriate.
...
<td>
<form action="post" action="the_name_of_your_php_script.php">
<input type="hidden" name="idUser" value="<?php echo $row['user_id']; ?>">
<input type="hidden" name="idTournament" value="<?php echo $row['tournament_id']; ?>">
<button type="submit" name="btn_register"></button>
</form>
</td>
...
Can't wrap it because <form> is not allowed as a child of <td>
All the documentation I see online permits forms inside of <td>. The other alternative is to use a link instead of a form.
You can use javascript to execute this functionality.
<table>
<tr>
<th>Tournament Name</th>
<th>Platform</th>
<th>Date</th>
<th>Venue</th>
<th>Judge</th>
<th>Description</th>
<th>Limit</th>
<th>Register</th>
</tr>
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['nTournament'];?></td>
<td><?php echo $row['pTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['vTournament'];?></td>
<td><?php echo $row['jTournament'];?></td>
<td><?php echo $row['dTournament'];?></td>
<td><?php echo $row['lTournament']; ?></td>
<td><button type="submit" onclick="sendRequest('<?php echo $idUser ?>','<?php echo $idTournament ?>')" name="btn_register"></button></td>
</tr>
<?php endwhile; ?>
</table>
Javascript code
function sendRequest(idUser,idTournament ){
// i am using jquery
$.post(other_page_url,{idUser:idUser,idTournament :idTournament},function(data)
{
window.location = 'tournamentlist.php';
} )}
I hope it's gonna help you
You don't need to wrap this in a <form> inside the table, you can do it all via Javascript / Jquery if you wish. On the html side, add the data you need to the button in data sets:
<td><button type="submit" name="btn_register" id="btn_register" data-user-id="<?php echo $userId ?>" data-tournament-id="<?php echo $row['idTournament']; ?>"></button></td>
Then in your script, you can call the data and send to post via ajax and either load the new page after INSERT, or load as a modal, or whatever you like. You can do something like below if loading a modal (change the load part to refresh the page if you want to go directly):
$('#btn_register').on('click', function () {
var t_id = $(this).data('tournament-id');
var u_id = $(this).data('user-id');
$.ajax({
url: "YourURL",
type: "POST",
data:{
t_id:t_id,
u_id:u_id
},
success: function (h) {
$(".modal-title").text("Table with new stuff");
$(".modal-body").html(h);
$('#modal').modal();
// lots of options here - you could even reload above if desired
}
})
});

Give the submit button a Php variable

I cannot find the solution to my issue on this forum. I'm trying to give the submit button a PHP value (unique ID) by extracting data from my database. I would also like to store the value in a SESSION. The submit button doesn't work when I I give it something other than a String as value.
Student View:
<pre>
<?php foreach ($tabinternships as $internship) { ?>
<tr>
<td><span class="html"><?php echo $internship->id_internship() ?></span></td>
<td><?php echo $internship->email_teacher1_internship() ?></span></td>
<td><?php echo $internship->email_teacher2_internship() ?></span></td>
<td><?php echo $internship->email_responsible_internship() ?></span></td>
<td><?php echo $internship->id_promoter_internship() ?></span></td>
<td><?php echo $internship->name_enterprise_internship() ?></span></td>
<td><?php echo $internship->stage_internship() ?></span></td>
<td><input type="radio" name="apply" value="<?php $tabinternship->id_internship() ?>"></td> <!-- Method calls an Object rather than an Array because I'm only displaying the attributes of 1 Object therefore the Array is not required here-->
</tr>
<?php } ?>
</pre>
StudentController:
<pre>
$tabinternships = Db::getInstance()->select_internships();
if(isset($_POST['apply']) && isset($_POST['application'])){
$_SESSION['apply']= $_POST['apply'];
$_SESSION['application']= $_POST['application'];
$_SESSION['redirect']='redirect';
}
if(!empty($_SESSION['application']) && !empty($_SESSION['apply']) && !empty($_SESSION['redirect'])){
$_SESSION['redirect']='';
header("Location: index.php?action=formdeux");
die();
}
</pre>
Thanks for your help.
I have found the error, I wasn't using echo when giving a value to the radio input
echo $tabinternship->id_internship()

PHP/jQuery - jQuery Plugin + PHP Foreach Returning Only One Working Result? | X-Editable For Twitter Bootstrap

There is a plugin called X-Editable where I it integrates into Bootstrap to have click to change Ajax requests. I have it all set up, and have it set to go, but my issue is that I have it in a foreach loop to echo out my users from my database, but it is only making the first row editable. screen
Here is my PHP Foreach,
<?php foreach($rows as $row): $userid = $row['id']; ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo htmlentities($row['username']); ?></td>
<td><?php echo htmlentities($row['email']); ?> </td>
<td><?php echo preg_replace('/([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+/', '\\1.**',$row['ip']); ?></td>
<td><?php echo htmlentities($row['timestamp'); ?></td>
<?php endforeach; ?>
Here is how I make the id "Username" editable,
<script type="text/javascript">
$('#username').editable();
</script>
The documentation for the plugin can be found here
Sorry if I show a lack of information, but I figure this is a pretty general question, why is the plugin only making the first result editable?
ID's must be unique, change id="username" to class="username", then do
$('.username').editable();
or do
<?php echo "username-".$userid; ?>
and then
$('[id^=username]').editable();

Use output from MySQL to extract additional info

How can I use the output of a MySQL query as to extract extra info from the database about that output?
For example, if my query generates a list of names and there is extra info about each name existing in the database, when I click on the name on the output page, the system will extract the relevant info from the database and show it on another page.
EDIT: Here's the code:
<?php
// you can delete mysql-assoc
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
?>
<tr align="center">
<td width="5%">
<a href="// I don't know what do put here " >
<?php print("{$row["pid"]}");?>
</a>
</td>
<td><?php print("{$row["place"]}");?></td>
<td><?php print("{$row["date"]}");?></td>
<td><?php print("{$row["ppp"]}");?></td>
<td><input type="button" value="Book"></td>
</tr> <?php } ?>
Set each row of the first output results equal to a variable, and then have an if statement in php that looks for strings in those variables, and then (upon some sort of a page refresh button since php is server-side code) displays the additional content.
Great; here's what you're missing:
<a href="<?php // I don't know what to put here ?>" >
Should be:
<a href="<?php echo 'database.php?action=viewrecord({$row["pid"]})'?>" >
From there, you should be able to extract the relevant info using the pid of the user.
On a side note, unless you're really worried about spacing, you should just dump out the relevant HTML; doing so will save you the task of repeatedly typing <?php ?> every time you want to dump out a variable.
<?php
// you can delete mysql-assoc
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
echo "<tr align='center'>";
echo "<td width='5%'>";
echo "<a href='database.php?action=viewrecord({$row['pid']})'>";
echo "{$row['pid']}";
echo "</a>";
echo "</td>";
echo "<td>{$row['place']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td>{$row['ppp']}</td>";
echo "<td><input type='button' value='Book'></td>";
echo "</tr>";
}
?>
Thanks for the help but this doesn't work :( I'm beginner in php when I press the record the url looks like localhost/ass/database.php?action=viewrecord%28{$row[
This isnt a direct answer to your question perse but since youre new to php im goign to through it out there. The syntax you uisng makes unicorns cry. It give me a headache jsut tryign to decipher it. Heres the same code in an alternative way:
<?php while($row = mysql_fetch_array($rs, MYSQL_ASSOC)): ?>
<tr align="center">
<td><?php printf('%s', url_encode('viewrecord('.$row['pid'].')'), $row['pid']); ?></td>
<td><?php echo $row['place']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['ppp']; ?></td>
<td><input type="button" value="Book"></td>
</tr>
<?php endwhile; ?>
However this database.php?action=viewrecord() worries me. I hope youre not using eval to execute a function passed by URL. Typically this would look something more like: database.php?action=viewrecord&id=2. You would verify that the action parameter is valid, and then invoke a function tied to that parameter which may or may not be the actual function name, and you would pass in the id parameter.
Eaxample database.php:
$action = isset($_POST['action']) ? $_POST['action'] : 'index';
switch($action) {
case 'viewrecord':
if(isset($_POST['id']) {
viewrecord((integer) $_POST['id']);
}
break;
// a whole bunch of other cases here for other actions
default:
// the default thing to do if an action is in valid
}
// the rest of your processing

PHP Repeating Region and Checkboxes

--Edited for clarity.
Database:
tblModule, contains a list of modules that can be enabled or disabled.
tblData, contains a list of trusts and the modules they have enabled. This links to tblModule on tblData.M01 = tblModule.mod_key
The PHP page is accessed from an index page and passes a variable lstModTrust to this page, to limit the returned records from tblData for a single trust. tblData.trust_key
A query runs, qryModuleList which returns a list of all modules. This is used to generate a table of all available modules. Each row shows module name tblModules.mod_name, module code tblModules.mod_code and a checkbox.
qryModData will return a list of the modules that are enabled for a single trust, and the corresponding checkboxes need to be ticked in the table.
This page will then be used to enable and disable modules for a trust. If a module is unticked the entry will be deleted from tblData, if it is ticked an entry will be inserted, and if no change then no change in the DB.
At the moment I'm having trouble getting the checkboxes ticked correctly based on qryModData
Any thoughts anyone?
--Edited to include code--
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Module</td>
<td>Module Code</td>
<td> </td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (!(strcmp($row_qryModData['M01'],$row_qryModuleList['mod_code']))) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)); ?>
</table>
Then there's two SQL queries, one that generates the list to build the table, and the second which I'm trying to use to set the boxes to ticked.
qryModuleList
SELECT *
FROM tblmodules
ORDER BY mod_name ASC
qryModData
SELECT *
FROM tbldata
WHERE trust_key = varTrust
varTrust is pulled from a URL variable.
Apologies for not including the code in the first place.
--Edit for new code.
<?php while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) { ?>
<tr>
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (strcmp($row_qryModData['M01'],$row_qryModuleList['mod_key']) != 0) {echo "checked=\"checked\"";} ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } ; ?>
--Edited for new Code.
<tr class="tblHead">
<td>Module</td>
<td>Module Code</td>
<td>Enabled\Disabled</td>
</tr>
<?php
$mod_data = array();
while ($row_qryModData = mysql_fetch_assoc($qryModData))
array_push($mod_data, $row_qryModData['M01']);
$currentRow = 0;
while ($row_qryModuleList = mysql_fetch_assoc($qryModuleList)) {
?>
<tr bgcolor="<?php echo($currentRow++ % 2)?"#CCFFFF":"#FFCCFF";?>">
<td><?php echo $row_qryModuleList['mod_name']; ?></td>
<td><?php echo $row_qryModuleList['mod_code']; ?></td>
<td><input <?php if (false !== (array_search($mod_data, $row_qryModuleList['mod_key']))) echo "checked=\"checked\""; ?> name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
</tr>
<?php } ; ?>
I think I'm understanding this now. What you need to do is place all of the allowed modules into an array and use the array_search() function to find it.
Example:
$mod_data = array();
while ($row_qryModData = mysql_fetch_assoc($qryModData)) array_push($mod_data, $row_qryModData['M01']);
That will get all the available modules into one array.
Next, while you cycle through the 'ModuleList' query, use the array_search() method to try and find the 'ModKey' variable in it. If you do, check the box. If not, do nothing.
Example:
<td><input <? if (false !== (array_search($mod_data, $row_qryModuleList['mod_code'])) echo "checked=\"checked\" "; ?>name="chkMod" type="checkbox" id="chkMod" value="<?php echo $row_qryModData['M01']; ?>" /></td>
(The reason I use a "!==" is because the function can return false or something that might equal false and might not. Read the page I've linked above for more)

Categories