Below is a piece of the code that input the results of a query into radio buttons. Then, when the user pushes one of the buttons in the form, it moves to the pages test.php. I have run into a problem though that the second code (which is on test.php) doesn't seem to work although I can't see any reason that it shouldn't. Any ideas are awesome. Thanks.
<?php while($row = mysql_fetch_assoc($result)): ?>
<tr>
<td><input type="radio" name="server" value="<?php echo $row['serverId']; ?>" /></td>
<td><?php echo $row['userName']; ?></td>
<td><?php echo $row['dateCreated']; ?></td>
<td><?php echo $row['serverName']; ?></td>
<td><?php echo $row['serverId']; ?></td>
<td><?php echo $row['publicDNS'] ?></td>
<td><?php echo $row['isRunning']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<br>
<?php endif; ?>
<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="server_stop" value="Stop Server"/>
<input type="submit" name="server_terminate" value="Terminate Server"/>
</form>
</body>
</html>
Second Code:
<?php
if (isset($_POST['server_stop'])) {
echo "Server Stopped";
}
if (isset($_POST['server_terminate'])) {
echo "Server Terminated"
}
?>
$_POST['submit'] has to be $_POST['server_stop']
<?php
if (isset($_POST['server_stop'])) {
echo "Server Stopped";
}
if (isset($_POST['server_terminate'])) {
echo "Server Terminated";
}
?>
Your code:
<?php
if (isset($_POST['server_stop'])) {
echo "Server Stopped";
}
if (isset($_POST['server_terminate'])) {
echo "Server Terminated"
}
?>
Both of these conditions are true on form submit. If you want to differentiate between which submit button was pressed, you'll need to make a hidden input and use some javascript:
<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="server_stop" value="Stop Server" onclick="whatwasclicked(this);" />
<input type="submit" name="server_terminate" value="Terminate Server" onclick="whatwasclicked(this);" />
<input type="hidden" name="clicked" id="clicked" />
</form>
Your javascript will be:
function whatwasclicked(c) {
$("#clicked").val() = c.value;
}
Now on the PHP side, you'll check $_POST['clicked'] and see which button was pressed.
EDIT:
Be aware there is Jquery being used here... so be sure to load that library.
If you don't want to use javascript at all, and you're only going to have those submit buttons in the form, you could also do this:
<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="clicked" value="Stop Server"/>
</form>
<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="clicked" value="Terminate Server"/>
</form>
Now just check for $_POST['clicked'] and you'll see which button was pressed.
if (isset($_POST['submit'])){
$name =trim($_POST['name']);
if (empty($name)){
echo "please input name";
}
else if (!preg_match("/^[a-zA-Z ]*$/",$name)){
$namerr = "Only letters and white space allowed";
}
}
Related
Hello for some reason job_id will not parse from myjobs.php to payment.php and I really cannot see how.Does anyone know why this may be?Similar files seem to work but for some reason this wont I am thinking maybe because I also have html code on payment.php which i haven't done before?
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['job_id']; ?></td>
<td><?php echo $row['title'];?></td>
<td><?php echo $row['description'];?></td>
<td><?php if($row['accepted']==1 AND $row['start_escrow']==0):?><form action = "payment.php">
<input type="hidden" value="<?php echo $row['job_id']?>" name="job_id" />
<input type="submit" class="btn btn-xlarge btn-block btn-primary" value ="Start Escrow"></input></input><?php endif; ?></td>
<td><?php if($row['start_escrow']==1):?><form action = "review.php">
<input type="hidden" value="<?php echo $row['job_id']?>" name="job_id" />
<input type="submit" class="btn btn-xlarge btn-block btn-primary" value ="Start Escrow"></input></input><?php endif; ?></td>
</tr>
<?php endwhile;?>
</table>
payment.php
<?php
require 'config.php';
$jobid = $_POST['job_id'];
$query = "UPDATE job SET start_escrow = '1' WHERE job_id = '$jobid''";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
echo "Thank You For Contacting Us <br>";
$conn->close();
?>
Set your method as POST in form.
<form action = "payment.php" method="POST">
<form action = "review.php" method="POST">
im currently displaying all the information from the table product in a tabular format, i have a button ADD which when click should add only the id, name and price from the table product to the table product_add in the same database. but my problem is that when i click on the button ADD, nothing is entered in the product_add table.
<?php
include'connect.php';
$image =$_GET['image'];
$id =$_GET['id'];
$name =$_GET['name'];
$price=$_GET['price'];
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0)
{
?>
<form method="post" id="form" name="form">
<table border='1'>
<?php
while ($row = mysql_fetch_array($result))
{
extract($row);
?>
<tr>
<td><?php echo $row['id']?></td>
<td><img src=<?php echo $row['image'] ?> /></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['price']?></td>
<td><input type='button' value='ADD' id="insert" name="insert"/></td>
</tr>
<?php
}
?>
</table>
</form>
<?php
}
if(isset($_REQUEST['insert']))
{
$insert = "INSERT INTO product_add(id, name, price)
VALUES ('$row[id]','$row['name']','$row['price']')";
$insertQuery=mysql_query($insert);
}
?>
</body>
</html>
I have updated the codes as shown below but the last row from the table product is being added to the table product_add. I want to add only a specific row when i click on the button submit.
<?php
include'connect.php';
$image = isset($_GET['image']) ? $_GET['image'] : "";
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$price= isset($_GET['price']) ? $_GET['price'] : "";
$sql="SELECT * FROM product";
$result = mysql_query($sql);
if($result>0){
?>
<form method="POST" id="form" name="form">
<table border='1'>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price MUR</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)){
extract($row);
?>
<tr>
<td><input name="id" value="<?php echo htmlspecialchars($row['id']); ?>">
</td>
<td><img src=<?php echo $row['image'] ?> width='120' height='100'/></td>
<td><input name="name" value="<?php echo htmlspecialchars($row['name']);
?>"></td>
<td><input name="price" value="<?php echo htmlspecialchars($row['price']);
?>"></td>
<td>
<input id="submit" type="submit" name="submit" value='Add to cart' />
</td>
</tr>
<?php
}
?>
</table>
</form>
<?php
}
if (isset($_REQUEST['submit']))
{
$insert = "INSERT INTO product_add(id, name, price) VALUES ('$id',
'$name','$price')";
$insertQuery=mysql_query($insert);
}
?>
Apart from the method (if your form uses POST, you should use $_POST in php), you do not have any form fields.
For example:
<?php echo $row['id']?>
Should be something like:
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
and:
<?php echo $row['name']?>
should be:
<input name="name" value="<?php echo htmlspecialchars($row['name']); ?>">
etc.
You should also switch to PDO or mysqli and prepared statements as the code you have now is vulnerable to sql injection. And ID's in html need to be unique.
One point is, you have multiple
<input type='button' ...>
with the same id="insert". ids must be unique within a web page.
The other thing is, you need a submit input to send the form
<input type="submit" ...>
From Submit Button state (type=submit)
The input element represents a button that, when activated, submits the form.
With <input type='button' ...> nothing happens, because it has no default action, see Button state (type=button)
The input element represents a button with no default behavior.
If you want an <input type='button' ...> to submit the form, you must do so by using some Javascript code.
One idea is to load content once the button is clicked.
js
$("#button").click(function() {
$("#holder").load("insert.php");
});
insert.php
$db->query("INSERT INTO table VALUES('one','two','three')");
Im getting crazy with this problem.
I try to use ajax to send data from a form in a way that I have not to reload all webpage (my webpage contain some queries for generate index menu that produce traffic); I create sendForm method, which handle form datas and pass it to php page to process.
This is the js code:
function sendForm(){
var http = false;
http = new XMLHttpRequest();
var ser = $(":input").serialize();
http.open("POST", "http://localhost/redir.php" ,true);
http.onreadystatechange= handleResponse();
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", ser.length);
http.setRequestHeader("Connection", "close");
http.send(ser);
return true;
}
My form data contain a variable numbers of inputs because there's some php code inside a for cycle.
<form name="form" id="form" class="table table-hover" method="post">
<?php while(.....)) { ?>
<tr style="line-height:40px">
<td><?php echo $home;?></td>
<td><?php echo $vis;?></td>
<td><input type="text" name="goal_h[]" class="input-mini"
<?php if ($sq[16] == "2"){ ?>
readonly value="<?php echo $goal_h;?>"
<?php }else if ($sq[16] == "3"){
?> readonly value="-"
<?php } else echo "value=\"-\"";
?> id="goal_h<?php echo $i?>[]" maxlength="2" size="2" />
<input type="hidden" name="id_sub" value="<?php echo $id_sub;?>" />
<input type="hidden" name="id" id="id" value="<?php echo $id_d;?>" />
<input type="hidden" name="data_gg" value="<?php echo $data_gg;?>" />
<input type="hidden" name="n_gg" value="<?php echo $n_gg;?>" />
<input type="hidden" name="id_p_home[]" id="idphome<?php echo $i?>" value="<?php echo $tmp_id_p;?>" />
<input type="hidden" name="id_p_vis[]" id="idpvis<?php echo $i?>" value="<?php echo $sq[10];?>" />
</td>
<td>
<input type="text" name="goal_v[]" class="input-mini" <?php if ($sq[16] == "2"){ ?> readonly value="<?php echo $goal_v;?>" <?php }else if ($sq[16] == "3"){ ?> readonly value="-" <?php } else echo "value=\"-\""; ?> id="goal_v<?php echo $i?>[]" maxlength="2" </td>
<td><input type="checkbox" <?php if ($sq[16] == "2"){ ?> checked <?php } ?> id="sosp<?PHP echo $i?>" name="sosp[]" value="<?PHP echo $i?>" onclick="cambiaStato(<?php echo $i?>);"/></td>
<td><input type="checkbox" <?php if ($sq[16] == "3"){ ?> checked <?php } ?> id="nd<?PHP echo $i?>" name="nd[]" value="<?PHP echo $i?>" onclick="cambiaStato(<?php echo $i?>);"/>
</td>
</tr>
<tr style="line-height:40px">
<td><input type="submit" id="submit" name="submit" value="AGGIORNA IL PUNTEGGIO" onclick="sendForm()" /></td>
<td colspan="5"></td>
</tr>
<?php } ?>
</form>
this code works if I don't use with ajax and simply use an action=name_of_file.php, but when I try to use sendForm() method, in redir.php file I lose $_POST variables.
$home_id_p=$_POST['id_p_home'];
$vis_id_p=$_POST['id_p_vis'];
$goal_home=$_POST['goal_h'];
$goal_vis= $_POST['goal_v'];
$sosp= (isset($_POST['sosp'])) ? $_POST['sosp'] : "99";
$nd= (isset($_POST['nd'])) ? $_POST['nd'] : "99";
and then I need to loop on $home_id_p.
I tried also wihout serialize(), and also with serializeArray() :
var ser = $(":input").serializeArray();
but with this I'm not able to send it, because I get an error "invalid argument" in
http.send(ser)
Any idea?
Thanks
<script type="text/javascript">
function removeLink()
{
document.getElementById("tab2").deleteRow(i);
}
</script>
</head>
<body>
<form action="forth.php" method="post">
<table width="600" border="1" id="tab2">
<?php
foreach($_POST as $post2)
{
?>
<tr>
<td>
<?php
echo $post2.'<br />';
?>
</td>
<td>Remove</td>
</tr>
<?php
}
?>
<tr>
<td><input type="submit" value="Next" /></td>
<td> </td>
</tr>
</table>
</form>
</body>
this is my page third.php it redirect user to new page forth.php on forth.php i have following code
<?php
print_r($_POST);
foreach($_POST as $key_post)
{
echo $key_post.'<br>';
}
?>
the issue is on page forth.php it doesn't print anything even when I did print_r($_POST); it returned me empty array like Array(), and help why data is not saved.
I assume it's empty because you don't actually have any <input name="foo" type="bar /> fields other than the submit button, so nothing is actually being posted.
You'll need to add some hidden fields:
foreach($_POST as $key => $post2)
{
?>
<tr>
<td>
<?php echo $post2.'<br />'; ?>
<input type="hidden" name="<?php echo $key;?>" value="<?php echo $post2;?>" />
</td>
</tr>
... etc
I am trying to create a function where users can check a message to delete. Obviously if more than one message is checked, they will all be deleted.
I have the following code
<form action="process.php" method="post">
<input type="hidden" name="deleteMessages" />
<?php
while($row=mysql_fetch_assoc($q))
{
if($row['to_viewed'] == 0)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></b></td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></b></td>
<td><b><?php echo $row['created']; ?></b></td>
</tr>
<?php
}
else if($row['to_viewed'] == 1)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></td>
<td><?php echo $row['created']; ?></td>
</tr>
<?php
}
}
?>
<input type="submit" value="Delete All" />
</form>
I want to pass the checkbox through and if the value is 1, process it and delete it.
But how would I achieve this with mulitple messages no matter if there is one message or ten?
Thanks
In your form put:
<input type="checkbox" name="check_box_delete[]" value="<?php echo $row['id']; ?>" />
Then to process:
if(isset($_POST['check_box_delete']))
{
foreach($_POST['check_box_delete'] as $id)
{
// Delete $id
}
}
You can let php store mutliple values in an array by naming the input fields with an array designator suffix [].
For example, all checkboxes named checkbox[] will then be stored in $_POST['checkbox'][].
Note that this may not be applicable to checkboxes as their $_POST value only exists if they were checked.
<input type="checkbox"
name="mid_to_delete[]"
value="some message id"
id="mid_to_delete_some_message_id">
<label for="mid_to_delete_some_message_id">Delete "Some subject"</label>
and then
<?php
foreach ($_POST['mid_to_delete'] as $mid) {
delete_some_message_id($mid);
}
?>
Since only successful controls are submitted, and unchecked checkboxes are not successful, all the values you get will be ones that have been selected for deletion. (Obviously you still need to perform auth/authz to make sure that the messages being deleted are ones the user is allowed to delete)