Give the submit button a Php variable - php

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()

Related

Dynamic href when generating buttons?

I have a table that is displaying data to the user and each row in the table has a unique id, in the table I am generating a button for each row to get more information on the content in the row. However, i need to generate a unique href for each button based on the Unique ID of the data in the row. The way I have done it it correctly goes to the link in the href but without passing the Unique ID.
<tbody>
<?php while($row = $query->fetch()) { ?>
<tr>
<td><?php echo $row['car_id']; ?></td>
<td><?php echo $row['car_make']; ?></td>
<td><?php echo $row['car_model']; ?></td>
<td><?php echo $row['number_available']; ?></td>
<td><?php echo $row['rent_cost_per_day']; ?></td>
<td><a class="btn btn-default" href="cars.php?"<?php $row['car_id']; ?>>View</a></td>
</tr>
<?php } ?>
</tbody>
Any help?
This is because you put the php after you closed the href quotes. You also have not put an echo function for the php, and have not put a get variable. You have:
href="cars.php?"<?php $row['car_id']; ?>
What you want to have is:
href="cars.php?id=<? echo $row['car_id']; ?>"

onclick redirect to url stored in php variable

I have the following table:
foreach ($forwards as $data):
?>
<tr style="cursor: pointer" class="main_row">
<td><?php echo $data['Offer']['id']; ?> </td>
<td><?php echo $data['Offer']['name'];?> </td>
<td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
<td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
<td><?php echo $data['Category']['name']?></td>
<td><?php echo $data['Country']['name'] ?></td>
</tr>
<?php
endforeach; ?>
now the idea is that when ever you click one of the main_row it should redirect to a new url. the problem is that the url contains an id for instance the url could look like www.example.com/details/2 where 2 is the id.
This id is like all of the other data stored in a php variable: $data['Offer']['id'];
Now my question how can i redirect using both php and javascript? is there a work around? .
Please do note that i am fully aware that php i server side and javascript is not. it is because of this i am asking this question.
<table>
<?php foreach ($forwards as $data): ?>
<tr data-link="http://www.example.com/details/<?php echo $data['Offer']['id']; ?>">
<td><?php echo $data['Offer']['id']; ?> </td>
<td><?php echo $data['Offer']['name'];?> </td>
<td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
<td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
<td><?php echo $data['Category']['name']?></td>
<td><?php echo $data['Country']['name'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<script>
$('table tr').click(function(){
document.location.href = $(this).data('link');
});
</script>
if you use jQuery though.
You can get the ID from the first column in the row:
$(".main_row").click(function() {
var id = $(this).find("td:first-child").text();
window.location.href = 'www.example.com/details/' + id;
});
If you use jQuery do this:
$('.main_row').click(function(){
window.location.href = "YOUR_URL"+$(this).attr('id');
});
And modify the html for the row to look like:
<tr style="cursor: pointer" class="main_row" id="<?php echo $id ?>">
This way - any time you click on the row it appends the row ID to the url and redirect you there.
If I am understanding you question correctly, your not looking for a redirect. To redirect in php, it must be done before you echo anything to the browser.
Because you want to "redirect" after your display is rendered, you should be using anchors.
<td><a href="www.example.com/details/<?php echo $data['Offer']['id']; ?>">
<?php echo $data['Offer']['id']; ?>
</a></td>
The anchor will direct the browser to the href url.

Checked box: Get all values, not only the last one (form, mysql and php)

I am having an issue with a form. I have a database, and with a form I get all the data from the clients with the php function foreach.
On the action page (invoice.php), I can display all the selected items, but every time I put the quantity for each items, it only displays the last text box I filled.
I don't know how to explain it any more, so here's my code:
form.php :
<input type="text" name="txtbox[<?php echo $input->id_article; ?>]" value="<?php echo $input->id_article; ?>" placeholder="1" style="text-align: center; width:30px;"></input>
<input
type="checkbox"
name="checked_articles[]"
value="<?php echo $input->id_article; ?>">
<?php echo "(".
($input->ref_article).")".
($input->nom_article)." (".
($input->prix_article)." €)"; ?><br>
<?php endforeach; ?>
invoice.php (header with the mysql data queries):
if(isset($_POST['checked_articles'])){
foreach($_POST['checked_articles'] as $chkbx){
$sql_articles = 'SELECT * FROM articles WHERE id_article="'.$chkbx.'"';
$req_articles = mysql_query($sql_articles) or die('Erreur SQL !<br>'.$sql_articles.'<br>'.mysql_error());
$data_articles[] = mysql_fetch_assoc($req_articles);
}}
$textbox = $_POST['txtbox'][$chkbx];
invoice.php (where the datas are displayed):
<?php foreach ($data_articles as $input) : ?>
<tr>
<td><?php echo $input['ref_article']; ?></td>
<td><?php echo $input['nom_article']; ?></td>
<td><?php echo $textbox; ?></td> <!-- Where I want the quantity to be displayed -->
<td><?php echo $input['prix_article']; ?> €</td>
<td><?php echo $input['prix_article']*$textbox; ?> €</td>
</tr>
<?php endforeach; ?>
I want to display, for each selected items, the quantity the users put on the text box.
Any help would be highly apprecied !
Thank you!
From the looks of your code, $textbox is an array filled with ids, like this:
Array
(
[txtbox] => Array
(
[1] => 1
[2] => 1
[3] => 1
)
)
In you foreach you need to reference the value with the article id, like this:
<td><?php echo $_POST['txtbox'][$input['id_article']]; ?></td>
Also, you should look into preventing sql injection. The id in you sql is passed directly from the post.
Hope it helps.

Update Multiple checkbox if it checked or not

I have a problem in updating multiple checkboxes
I have the following ,
if the price is checked then I will calculate its total price if not then the total price is 0
so I have this in the view
<?php echo form_open("/lab-forms/save/$id_form") ?>
<table>
<tr>
<td><input type="checkbox" name="approved[]" <?php if($price->is_checked==1) { echo 'checked="checked"';}?> value=<?php echo $price->id; ?>></td>
<td><?php echo $price->price; ?></td>
<td><?php echo $price->qt; ?></td>
<td><?php echo $price->total_price; ?></td>
</tr>
</table>
</form>
I want to get the checked and the unchecked ones cause the default on the database is checked
Thanks
echo ($price->is_checked == 1 ? "checked='checked'" : "")
This is exactly like
if($price->is_checked == 1) {
echo "checked='checked'";
}
else {
....you know....
}
Just easier yo use for situations like these one

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