PHP - database query based - php

I have built a table in PHP to display a list of people and data relating to them. I want the user to be able to select one of those users to send an e-mail request to. So I have put a button at the end of each row for every user.
Im not sure how to write the PHP script to identify which button has been pressed. I have tried assigning the name of the button to be the e-mail address of the user for the row but I dont know how to read that value in the corresponding script...
Code for initial page:
if ($query_run = mysql_query($query))
{
echo "<fieldset>";
echo "<table>";
while ($row = mysql_fetch_assoc($sqldata))
{
echo "<tr><th>Name</th>";
echo "<td>";
echo $row['FirstName']." ".$row['LastName'];
echo "<td>";
echo $row['Country'];
echo "<td>";
echo $row['Profile'];
echo "<td>";
echo $row['Email'];
$Email = $row['Email'];
echo "<td>";
echo '<input type = "submit" name="$Email" value="connect">';
}
echo "</table>";
echo "</fieldset>";
Thanks!

If you are planning to do this in HTML5, you could alter the submit button, so that it would redirect to a page, where you can get the e-mail by $_GET[];
echo "<input type=\"submit\" formaction=\"request.php?email=".$Email."\" value="Submit">\";
Then in your request.php you could use:
$email = $_GET['email'];

I think, you have to change name attribute from "$Email" to, I don't know, maybe "user_email" and assign $email variable to value attribute. So in script which must handle this form you will just need to retrieve a value of $_POST['user_email'].
Also I think it's better to use $Email in double quotes:
echo "<input name='$Email'" ...>;
because you will get string '$Email' instead of expected email address.
Or you can create a form for each table row.

There are any number of ways you could approach this, but I would personally choose to use an HTML form for this purpose. Consider the following HTML:
<form name="myForm" method="GET" action="">
<button type="submit" name="email" value="test1#email.web">test 1</input>
<button type="submit" name="email" value="test2#email.web">test 2</input>
<button type="submit" name="email" value="test3#email.web">test 3</input>
</form>
If you were to try to click on one of these buttons, you'd notice your query string will now show ?email=test2%40email.web (assuming you clicked the second button).
The gist of this approach is to use the same name attribute for your submit buttons, but assign a different value to each. I chose button over input due to the ability to present button text different from the value attribute (as described in this question).
Applying this principle in your PHP, you'd get something like:
while ($row = mysql_fetch_assoc($sqldata))
{
echo "<tr><th>Name</th>";
echo "<td>";
echo $row['FirstName']." ".$row['LastName'];
echo "<td>";
echo $row['Country'];
echo "<td>";
echo $row['Profile'];
echo "<td>";
echo $row['Email'];
$Email = $row['Email'];
echo "<td>";
echo '<button type="submit" name="email_address" value="' . $Email . '">Send email</button>';
}
Then, you just check your email_address parameter for the email address.

Related

I am sending data through form, it is not working

I have this delete function in my system but first I need the server needs to know which table he has to delete that is why I am sending a data when the user click the delete button into the server which is the ID of the data I want to delete..first I need to try and get that data being sent by the form but the problem is sending data is not working in my part I tried to echo out the ID just to see if I have a result but it works fine but when I send it to the server it doesn't print anything.. Here is my code where I fetch the scheduleID and the form
if ($strand<>""){
$query1 = mysqli_query($conn,"SELECT * from schedule natural join instructor where day = 'm' and schedule.strand= '$strand' and timeID ='$id' and grade = '$grade' and semester = '$semester'");
}
$row1 = mysqli_fetch_array($query1);
$schedID = $row['scheduleID'];
$id = $row1 ['scheduleID'];
$count=mysqli_num_rows($query1);
if ($count==0)//checking
{
//echo "<td></td>";
}
else
{
//print
echo "<div class='show'>";
echo "<ul>
<li class='options' style='display:inline'>
<span style='float:left;'><a href='sched_edit.php?id=$id1' class='edit' title='Edit'>Edit</a></span>
<span class='action'><a href='#' id='$id1' class='delete' title='Delete'>Remove</a></span>
</li>";
echo "<form class = 'delete' method = 'post' action ='../functions/delete.php'>";
echo "<li class='showme'>";
echo " <input type='hidden' name='delete' value='$id'>";
echo "<button type='submit' name='delete' class='btn btn-danger'>Display Schedule</button>";
echo $row1['subject'];
echo "</li>";
echo "<li class='$displayc'>$row1[strand]";
echo "<li class='$displaym'>$row1[fname], $row1[lname]</li>";
echo "<li class='$displayr'>Room $row1[room]</li>";
echo "</form>";
echo "</ul>";
echo "</div>";
}
?>
</td>
I tried the hidden attribute in some of my forms and it work there but I don't know why it won't in this form, the $id is working also I tried echoing that inside in this page but the data sent is not printing in the server, here's my server
<?php
session_start();
include 'database.php';
if (isset($_POST['delete'])){
$ID = $_POST['delete'];
}
thanks in advance
The problem here is that you have 2 form elements using the same name attribute, so PHP is only keeping the last value, which is not defined.
See both the <input> and the <button> have the same name! The button is last and has no value; that is what is being used by PHP.
<input type='hidden' name='delete' value='$id'>
<button type='submit' name='delete' class='btn btn-danger'>Display Schedule</button>
So you can just remove the name from the button or change it to something other than delete :-)

the form clears the array everytime I send it

I have a form with 2 selects, when you send the first, the second select charges the values that are called on my oracle bd with a query, then when i send the second select, it generates a table with checkboxes:
if(isset($idTActi)){
$stallTableTarifas=oci_parse($conn, "SELECT TARIFAS.ID, TARIFAS.ID_TIPO_ACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, TIPO_ACTIVIDAD
WHERE TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TARIFAS.ID_TIPO_ACTIVIDAD = $idTActi");
oci_execute($stallTableTarifas);
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Tipus Tarifa</td>
<td>Temps/Km</td>
<td>Preu</td>
<td><input type='submit' class='carrito' value=''></td>";
echo "</tr>";
while (($row=oci_fetch_array($stallTableTarifas,OCI_BOTH))!=false){
echo "<tr>";
echo "<td>".$row['TIPO']."</td>";
echo "<td>".$row['TEMPS_KM']."</td>";
echo "<td>".$row['PRECIO']."</td>";
echo "<td><input type='checkbox' name='checkbox[]' value='".$row['ID']."'/></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
echo "</form>";
The variable $idTActi it's the id that i return from the second select, so when i click on the checkboxes and i send it on the button named class='carrito', that's an sprite that i generate on css, i see on the bottom another table with the information that i selected on the previous table:
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
foreach($_POST['checkbox'] as $item){
$stallTableCarrito=oci_parse($conn, "SELECT ACTIVIDAD.NOM AS NOM_ACTIVIDAD, TIPO_ACTIVIDAD.NOM AS NOM_TACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, ACTIVIDAD, TIPO_ACTIVIDAD
WHERE TARIFAS.ID = $item
AND TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TIPO_ACTIVIDAD.ID_ACTIVIDAD = ACTIVIDAD.ID");
oci_execute($stallTableCarrito);
$array=array(
0=>array(),
1=>array(),
2=>array(),
3=>array(),
4=>array()
);
while (($row=oci_fetch_array($stallTableCarrito,OCI_BOTH))!=false){
array_push($array[0],$row['NOM_ACTIVIDAD']);
array_push($array[1],$row['NOM_TACTIVIDAD']);
array_push($array[2],$row['TIPO']);
array_push($array[3],$row['TEMPS_KM']);
array_push($array[4],$row['PRECIO']);
}
for ($x=0;$x<count($array[0]);$x++){
echo "<tr>";
echo " <td>".$array[0][$x]."</td>";
echo " <td>".$array[1][$x]."</td>";
echo " <td>".$array[2][$x]."</td>";
echo " <td>".$array[3][$x]."</td>";
echo " <td>".$array[4][$x]."</td>";
echo " <td><input type='submit' class='carritoElim' value=''></td>";
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
Basically that's a shopping form.
And where is the problem? When i send the pushed checkboxes with the button class='carrito', the form by default refresh the page and clears my array, what can i do?
In your first part of code, is your form tag open ? (I guess it is if this one works)
In the second part, is your <input type='submit' class='carritoElim' value=''> tag in a form ?
Because if it's not, you gonna have a bad time ;-)
Maybe in the last form you should generate hidden input with same names as your first form and same values.
If you don't I guess your variable $idTActi won't be set anymore and it won't succeed the first test if(isset($idTActi)). That could be why you get a cleared page.
If you have a multi step form in the same php page, for this kind of html code :
<form method=POST url="myURL">
<select name="select1">[...]</select>
<select name="select2">[...]</select>
<!-- VARIOUS PART : may not be displayed -->
<div id="checkboxes">
<input type="hidden" name="boxStep" value="1"/>
<input type="checkbox" name="cb1" value="1"/>
[...]
</div>
<!-- END OF VARIOUS PART -->
</form>
Then you need php tests in this order :
// if post request
if (isset($_POST)) {
if (isset($_POST['boxStep'])) {
// behavior when checkboxes values are sent
} else {
if (isset($_POST['select2'])) {
// behavior when second select is filled
// display "VARIOUS PART"
} else {
// behavior when only first select is filled
// Do not display "VARIOUS PART"
}
}
} else {
// default behavior (no select filled)
// Do not display "VARIOUS PART"
}
Apolo

A button result in a mysqli_fetch_array cannot get through to the next page

I have a php page with this code that passes a variable of a button to next page:
<div><center><table>
while($row = mysqli_fetch_array($result))
{
echo "<td><form action= display.php method= 'post'><input type='hidden'
name='projectid' value=".$row['projectid'].">
<input type= 'submit' name= 'type'
value= 'View/Amend Project Details'></form></td>\n";
}
echo "</table></div>";
I have this on my next page in a table:
$projectid= $_POST['projectid'];
echo "<td>" . $row['projectname'] . "</td>";
I still cannot see the problem, any idea?
The problem is that you're trying to use the $row on your second page, and it isn't set there.
You have to either do the mysqli query again, or transfer the value of $row['projectname'] through the form, using a hidden input field.
<?php
while($row = mysqli_fetch_array($result,,MYSQLI_ASSOC))
{
echo " name='projectid' value=".$row['projectid']."> value= 'View/Amend Project Details'>\n";
}

Pass two variables to URL on page reload

On my page, I have two dropdown boxes: One for a list of dates, and one for options. The box for dates is populated as such:
echo "<form name='selectedDate' action='page.php' method='GET' class='auto-style1'>";
echo "<select name='date' onchange='this.form.submit()'>";
echo "<option value=select>Select a Date</option>";
while ($row = mysql_fetch_array($result))
{
$date = date("m-d-Y",strtotime($row['date']));
if($date==date("m-d-Y"))
{
echo "<option>TODAY</option>";
}
else
{
echo "<option>" . $date . "</option>";
}
}
echo "</select>";
echo "<br />";
echo "<noscript>";
echo "<input type='submit' value='Select'>";
echo "</noscript>";
echo "</form>";
The other dropdown is populated as:
echo "<form name='selectedFormat' action='page.php' method='GET' class='auto-style1'>";
echo "<select name='option' onchange='this.form.submit()'>";
echo "<option value=select>Select a Option</option>";
echo "<option value=Artist>Artist</option>";
echo "<option value=Song>Song</option>";
echo "</select>";
echo "<br />";
echo "<noscript>";
echo "<input type='submit' value='Select1'>";
echo "</noscript>";
echo "</form>";
Now, you notice that both of them are set for Submit() upon change. If I change the first one, the URL in the browser becomes page.php?date=02-26-2013, and if I change the second one, the URL in the browser becomes page.php?option=Song.
The problem that I'm having, is that if I change the date to an older date, and then change the option, my page reverts back to the date being the current date. I've tried setting the action to a different URL:
$url = "musicInfo.php?date=" . $nonFormatedDate . "&option=" . $option;
And I changed both forms to have:
echo "<form action='" . $url . "' method='GET' class='auto-style1'>";
If I echo $url;, it correctly displays what the browser URL should be. However, the browser URL is still populated by whichever dropdown I used last.
My question here is, how do I get both variables to be passed upon either submission?
So for instance, if I change the date to be 01-13-2012, and then change the option to Song, how can I ensure that the date will be passed as well? And vice versa. Or, say if I change the date to 01-12-2012, is it possible to have the date dropdown box have this selected upon reload?
Site I'm trying to do: http://www.legendary-immersion.com/random/test.php
As people say in comment you can submit only one form per request.
So the best way is to hide data that you need in your form.
In your php file add this code
$date = isset($_GET["date"]) ? $_GET["date"] : "";
$option = isset($_GET["option"]) ? $_GET["option"] : "";
Then in first form add
<input type="hidden" name="option" value="<?php echo $option; ?>"/>
And in the second form add
<input type="hidden" name="date" value="<?php echo $date; ?>"/>
As the result in every form submit you will get the url like
page.php?date=yourdate&option=youroption
Best regards, Hope this will help

a array of php with multiple textboxes

basically i have a database, which contains a random amount of questions
i have then printed this out as a php table using a query. there is as many textboxes to input marks as there are questions in the database.
I want to create an array of my inputted data and then update the database with the marks in.
Here is my code
echo "<table border = '0' cellpadding ='10px'>";
echo "<tr>
<td> Question <td>Mark</td><td>Criteria</td>
<td>Feedback</td>
</tr>";
while ($row = mysql_fetch_array($result))
{
$question[]=$rows['question'];
echo "<tr>";
echo "<td>". $row['question']. "</td>";
echo "<td>" ."<input type = 'text' name = 'mark[]' size = '1' value = '0' id = 'mark'/>/". $row['maxMark'] . "</td>";
$maxMark[] = $row['maxMark'];
echo "<td>".$row['criteria']."</td>";
echo "<td>" . "<textarea name = 'feedback[]' id= 'feedback'>Enter Feedback here</textarea>". "</td>";
echo "</tr>";
}
echo "</table>";
echo "</tr>\n";
echo "</table>";
I am not sure how to create the array, with the marks you input. please help.
In short i want to populate an array with the marks i input
This is rather easy to be done. All you have to do is setting the attribute name of your input controls and add an index.
Example:
<input type="text" name="name[0]" /><input type="text" name="mark[0]" />
<input type="text" name="name[1]" /><input type="text" name="mark[1]" />
The post (or get) data your script receives will then contain an array instead of a single variable.

Categories