I'd like to ask for help about my code. I'm still beginning to find way with html, php, databases and all that. This is something I already did some times, but somehow I got stuck at this point.
The connection is fine, so I omitted it.
The thing is that my buttons don't do anything when I click them, it's like there's no action to be taken. At this point I can't notice my mistake, but I'm sure it's very simple. :(
I've translated some things from the code from my native language to English, so if you find it inconsistent in that regard, I'm sorry. *
I'd guess it's something I've messed up on the table structure
Thank you!
<?php
echo "<th>City |</th>";
echo "<th>Update |</th>";
echo "<th>Delete |</th>";
echo "</tr>";
$query = "SELECT name, id_city as id FROM city";
$result = pg_query($conn, $query);
if($result) {
while($row = pg_fetch_assoc($result)) {
echo '<tr>';
echo '<td>';
echo $row['name'];
echo '</td>';
echo '<td>';
echo '<form method="post" action="./updatecity.php">';
echo '<input type="hidden" name=id_city value="'.$row['id'].'">';
echo '<input type="submit" name="submit" value="Update">';
echo '</form>';
echo '</td>';
echo '<td>';
echo '<form method="post" action="./deletecity.php">';
echo '<input type="hidden" name=id_city value="'.$row['id'].'">';
echo '<input type="submit" name="submit" value="Delete">';
echo '</form>';
echo '</td>';
echo '</tr>';
}
}
pg_close($conn);
?>
Your approach to making form is completely wrong, what you are doing in simply echoing texts, No form is getting generated. Hence the button is not working because there is no button.
What you need to do it something like this :
<?php
#Write your php related code here
#Like connecting to database
?>
#All your html related content goes here like making tables
<form method="post" action="./updatecity.php">
<input type="hidden" name=id_city value="<?php echo $row['id'] ?>">
<input type="submit" name="submit" value="Update">
</form>
<?php
//Write your php related code here
?>
Related
I'm using php to dynamically create multiple buttons on my web page that have increasing values 01 to 09. When I create the forms as below the buttons are created as expected with the correct values but when I submit the form an empty array is posted.
The code here is inside a for loop that increments $i:
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="RunSmokeTest" value="0'.$i.'">';
echo '<input type="submit" name="submit" value="Run Test">';
echo '</form>';
Here is the HTML that is created:
However, when I change the code to take out the $i variable, the form will be submitted as expected but all the buttons will have the same value which I can't have.
Here is the start of test.php that prints out array(0) { } when I click the submit button and an alert comes up with message 'empty'.
var_dump($_POST);
if(empty($_POST['RunSmokeTest']))
{
echo '<script language="javascript">';
echo 'alert("empty")';
echo '</script>';
}
else{
...
}
Try this .
<?php
if(isset($_POST['submit'])) {
echo $_POST['RunSmokeTest'];
}
for ($i=0; $i < 10; $i++) {
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="RunSmokeTest" value="0'.$i.'">';
echo '<input type="submit" name="submit" value="Run Test">';
echo '</form>';
}
?>
Demo is here
I am trying to build shoutbox using php and mysql only.
I am using the code below:
<?php
$sqldisplay =$Db1->query("select * from shoutbox ORDER BY date_time DESC");
?>
<h4>shout box input here</h4>
<form method="post" action="">
Message: <input type="text" id="message" name="message" class="message" />
<input type="submit" id="submit" value="Submit" name="shout" />
</form>
<?php
if(isset($_REQUEST['shout']))
{
$message = htmlspecialchars(mysql_real_escape_string($_POST['message']));
$sqlact =$Db1->query("Insert into shoutbox Values(NULL,NOW(),
'$username','$message')");
echo "save db";
}
?>
<table>
<thead><tr><td colspan="3">
<center>shout box output here</center></td></tr><tr><th>date</center></th>
<th><center>username</center></th>
<th><center>message</center></th></tr></thead>
<?php
while($row = mysql_fetch_array($sqldisplay))
{
echo "<tr> ";
echo "<td>" .$row[date_time] . "</td>";
echo "<td>" .$row[name] . "</td>";
echo "<td>" .$row[message] . "</td>";
}
echo "</tr> ";
?>
</table>
I know there are lots of jquery shoutbox available on net, but i have no idea about jquery. so posting my question here.
Problems:
1: I want the output to be displayed right after user press submit. his shout should also appear in the table with out refreshing the page
2:I want the output to displayed in scrolling manner as does the normal shoutbox shoul look like.e.g.
http://skrypty.klocus.pl/2012/01/php-ajax-shoutbox.html
Someone generous enough to help me in building this little script.
This is my code for creating an html form that reads from a database and will allow the user to check and uncheck boxes for each of the 640 items. This is the form.php:
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body> <table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}
The user must then be able to click on submit and have those values updated in the mysql database.
Right now when I click the submit button, it posts to edit form.php which has this:
<?php
//echo results
foreach($_POST['stickerID'] as $k=>$v ){
echo $k;
echo $v;
}
?>
But I don't get anything echoed. I was thinking the problem could be that Im actually creating a form for every row instead of 1 form with many rows/checkboxes. But when I move the form code after the and the tag to the line where line, I can't even load the form.php, it just loads blank.
Where is my problem? :) Thx
Name your checkbox like this:
<input type="checkbox" name="stickerID[]" value=" <?php echo $row['stickerStatus']; ?> ">
And as Amal already said update your code to PDO or MySQLi
you can do this with a tag :-
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="checkbox[]" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
on your php code you get :-
$all_checkes_checkbox = $_POST['checkbox'];
here is your all checked checkbox:-
and this array also bale key and value
I'm new to php and I'm trying to pass a the value of a submit button through a session.
my code so far looks like this:
main_page.php
session_start();
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
$value_to_pass = $_POST[submit];
echo $value_to_pass;
$_SESSION['value'] = $value_to_pass;
}
details_page.php
session_start();
echo $value_to_pass = $_SESSION['value'];
echo $value_to_pass;
I need it to print $value_to_pass in the details_page.php
This is highly confusing
session_start();
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
$value_to_pass = $_POST[submit];
echo $value_to_pass;
$_SESSION['value'] = $value_to_pass;
Consider changing it to this so that your POST code only gets executed when the form is actually submitted. Without this check your SESSION will be assigned a blank value when the form is not submitted which may be giving you odd results.
session_start();
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
// Note also you need single quotes in the $_POST array around 'submit'
if(isset($_POST['submit']))
{
$value_to_pass = $_POST[submit];
echo $value_to_pass;
$_SESSION['value'] = $value_to_pass;
}
And change details_page.php to
session_start();
// Do not echo this line, as you are echoing the assignment operation.
$value_to_pass = $_SESSION['value'];
var_dump($value_to_pass);
first You change your below code
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
with the below
echo '<form method="post" action="details_page.php">';
echo '<input type="hidden" name="ip_address" id="ip_address" value="'.$ip_address.'">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit /></a></li>';
echo '</form>';
this is the best practice actually. I also followed this. So try to avoid passing values in "submit" button. pass it to "hidden" field.
and then get the value like below:-
$value_to_pass = $_POST["ip_address"];
I think it will help You. Thanks.
I've written a script to post information into my database for I can't get it to post and need help pointing out what I'm missing.
my php post for looks like this:
$query = "SELECT * from departments";
$res = mysql_query($query);
echo '<div id="department" >';
echo '<form action="depedit.php" method="post">';
echo '<input type="text" placeholder="Search departments">';
echo '<br>';
echo '</form>';
echo '</div>';
echo '<div id="depadd">';
echo '<form>';
echo '<table width="0" border="0">';
echo '<tr>';
echo '<td>Name:</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo "<select name='depid'>";
while ($row = mysql_fetch_array($res)) {
echo "<option value='".$row['id']."'>".$row['depname']."</option>";
}
echo "</select>";
echo '</td>';
echo ' </tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo ' <tr>';
echo '<td><label class="limit">Select Limit for active courses in Learning Locker:</label></td>';
echo ' </tr>';
echo ' <tr>';
echo '<td>';
echo "<select name='courselimit'>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "<option value='3'>3</option>";
echo "<option value='4'>4</option>";
echo "<option value='5'>5</option>";
echo "<option value='0'>Unlimited</option>";
echo "</select>";
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="radio" id="1" name="senabled" value="1"/><label for="c1" class="required">Required<br>(Study Shredder Feature Enabled)</br></label></td>';
echo '<td><input type="radio" id="0" name="senabled" value="0"/><label for="c1" class="optional">Optional</label></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="orgid" value="'.$adminorgid.'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="createdby" value="'.$userid.'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="timecreated" value="'.time(now).'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo ' <tr>';
echo ' <td><button type="submit" class="btn">Submit</button></td>';
echo ' </tr>';
echo '</table>';
echo "</form>";
echo '</div>';
depedit.php looks like this:
$adddep = "INSERT INTO organization_dep (orgid,depid,courselimit,senabled,createdby,timecreated) VALUES ('".$_POST["orgid"].",".$_POST["depid"].",".$_POST["courselimit"].",".$_POST["senabled"].",".$_POST["createdby"].",".$_POST["timecreated"]."')";
$res = mysql_query($adddep);
if ($res === TRUE) {
echo "Department added successfully";
} else{
printf("Could not create department");
}
the correct information seems like its getting passed as the url displays the following information in it:
/index.php?depid=6&courselimit=3&senabled=1&orgid=9&createdby=1129&timecreated=1364005206
any help with this would be greatly appreciated as I'm sure its something simple that I'm just over looking.
You want to get the values from the $_POST
('".$_POST["orgid"].",".$_POST["depid"].",".$_POST["courselimit"].",".$_POST["senabled"].",".$_POST["createdby"].",".$_POST["timecreated"]."')";
but your saying it is passed through the URL, this looks like you need to use $_GET to get the values
--- and one small tip you can use echo to print multiple lines it works. as long as you close it, in the end
e.g
echo " <html>
<body>
</body>
</html>
";
I think you have a bunch of things to get right before this will be working:
Only the first form, which seems to be for searching departments, have method and action attributes
Since the second form does not have the action attribute, it is not submitting to depedit.php but to the same page that has the form.
Since the second form does not have the method attribute, it defaults to GET, and you are trying to read out POST variables in your PHP. If it was not using GET, you would not see those params in the resulting URL.
In your SQL insert statement, you must have single quotes around every single text value but not around int values. Now you have one single quote before the first value and one before the last which makes no sense.
First: this portion will not be POSTed "depedit.php" because there is no submit
echo '<form action="depedit.php" method="post">';
echo '<input type="text" placeholder="Search departments">';
echo '<br>';
echo '</form>';
Second: this will never be POSTed to depedit.php, since your <form> does not have an action specifying "depedit.php"
echo '<form>';
echo '<table width="0" border="0">';
echo '<tr>';
//other codes
echo "</form>";
maybe you mean to remove the first echo '</form>'(line 7) and the second <form>(line 10)