I have the following form in php:
<?php
<form action='deletepost.php' method='post'>
<input type='hidden' name='var' value='$comment_id;'>
<input type='submit' value='Delete'>
</form>
?>
when the user press the delete button goes to deletepost.php script which is the following:
<?php
$comment = mysql_real_escape_string($_POST['var']);
if(isset($comment) && !empty($comment)){
mysql_query("UPDATE `comments` SET `flag`=1 WHERE (`user`='$session_user_id' AND `comments_id`='$comment')");
header('Location: wall.php');
}
?>
I want the delete button that I have in my form to make it look a link. Any idea which is the best and easier way to do this?
#kumar_v did it. Only for example (use real link):
<form action='deletepost.php' method='post'>
<input type='hidden' name='var' value='$comment_id;'>
<a onclick="javascript:this.parentNode.submit();">Delete</a>
</form>
Assign a class to your submit button called "btn_link".
html:
<input type='submit' value='Delete' class='btn_link'>
Then you can do with css
.btn_link
{
background:none!important;
border:none;
padding:0!important;
border-bottom:1px solid #444; /*border is optional*/
}
N.B.: See the comments below, regarding the use of !important
Related
<form method='get' action='y.php'>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['btnSave'])) {
$name=isset(($_GET['txtName'])?isset($_GET['txtName']:'');
//then Logic of insert goes here
}
}
?>
so before moving to y.php the record must be saved.
but I cant get the $name value, as action given to y.php.
How can I get $name which contain value in text box.
if you change the action to this (same/current) page record is going to database without any flaw or error.
try using post method instead and change your code accordingly, try this:
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit' name='submit'/>
</div>
</form>
<?php
if (isset($_POST['submit'])) {
$name=$_POST['txtName'];
//then Logic of insert goes here
//redirect to y.php with name value
echo "<script>window.open('y.php?user=$name','_self')</script>";
}
?>
Then use $nme = $_GET['user']; to get the value of $name in y.php
Try this code,
<form method='get' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['txtName'])) {
$name=$_GET['txtName'];
//then Logic of insert goes here
}
}
?>
I suggest you use method post. You can code like this
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if(isset($_POST['txtName'])){
$name =$_POST['txtName'];
echo $name;
}
?>
you can use the session if you want make the value use in another file.
I hope that can solve your problem
I have a page that prints out rows of information. In each row is a notes box:
<?php
<td style='font-size:12px;width:300px;'>
{$row['Notes']} <br /><center><br />
<form action=\"http://********/functions/notes.php\" id='formNotesform' method='post'>
<input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
<textarea placeholder=\"Add more notes here...\" name=\"notes\" rows=\"5\" cols=\"30\"'></textarea><br />
<input type='submit' name='formNotes' id='formNotes' value='Add to Notes' />
</form>
</center></td>
?>
Then there's also another button on the page in each row.
<form action=\"http://********/functions/archive.php\" method='post' id='formArchiveform' onclick=\"return checkArchive()\">
<input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
<input type='submit' name='formArchive' id='formArchive' value='Archive' style=\"height:25px; width:80px\"/>
</form>
What I need to happen is that when someone clicks the "Add to Notes" button it does its job but when someone clicks the "archive" button it checks to see if notes is empty and if not then it submits that as well (kind of like a failsafe).
Ideally I'd like to just pick up the Notes data and post it to the archived.php file the form is going to anyways since that would cause minimal disruption to the code base but I can't get it to work.
I understand this isn't really a sensible choice. It just has to be done. Thanks for the help!
If I had reputation I would ask first because what I understood is that you want to now what button has been pressed to then do another things.
If that's it do this:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['formNotes']))
{
//...
}
elseif(isset($_POST['formArchive']))
{
//...
}
}
What is the best way to implement php code to run when
a button is pressed/clicked. Done some research but couldn't find anything relevant apart of few examples that show use of JavaScript.
I want it to be purely in PHP.
At this precise moment I have created the buttons in the following way <input type='button' value='Click Me'/> now not sure what to do. Could anyone help please.
I don't think you can run a PHP script by native HTML only. Using JavaScript would be the simplest solution you can do.
The basics of a form and php. when the button is pushed, the form calls an action (some page, even itself). Then the php checks if the button was pushed. If so, do something, if not, ignore it. Instead of creating a button though, a submit would work better, but the button works just fine.
<form method="post" action="./PHPScripts.php" />
<input type='button' value='Click Me' name='button' /> <-- added a name
<input type='submit' value='Click Me' name='submitButton' />
<input type='submit' value='Click Me' name='submitButton2' />
</form>
// below would be on the page called by the action in the above form
// which can be on the same page with no problem, but larger amounts of
// code become more difficult to read, so having multiple files is recommended
<?php
if($_POST['button']) {
// php code (either execute it, or call function)
}
if($_POST['submitButton']) {
// php code (either execute it, or call function)
}
if($_POST['submitButton2']) {
// php code (either execute it, or call function)
}
?>
It's best to keep php code on top of html if on same page, but for demonstration reasons, I've put it on the bottom. (Easier to read)
The idea is, you will need to give the button the name attribute. Depending on how the form, is then submitted, in this case, to itself or to another page determined by the form action attribute and the method, the script will then run.
<form action="submit.php" method="post">
<input type='button' value='Click Me' **name="submit"**/>
</form>
if(isset($_POST['submit']) {
//runs when the button is clicked
}
<form action="index.php" method="post">
<input type='button' name='btn_func' value='Click Me'/>
</form>
<?php
if(isset($_POST['btn_func'])){
//call the function here.
}
?>
If you have 10 buttons:
<form action="index.php" method="post">
<input type='button' name='btn_func_1' value='Click Me 1'/>
<input type='button' name='btn_func_2' value='Click Me 2'/>
<input type='button' name='btn_func_3' value='Click Me 3'/>
<input type='button' name='btn_func_4' value='Click Me 4'/>
<input type='button' name='btn_func_5' value='Click Me 5'/>
<input type='button' name='btn_func_6' value='Click Me 6'/>
<input type='button' name='btn_func_7' value='Click Me 7'/>
<input type='button' name='btn_func_8' value='Click Me 8'/>
<input type='button' name='btn_func_9' value='Click Me 9'/>
<input type='button' name='btn_func_10' value='Click Me 10'/>
</form>
<?php
if(isset($_POST['btn_func_1'])){
//call the function here.
}
?>
Or use a Conditional/Switch statement.
If you are going to use input type="button", I am assuming you will need JavaScript to handle the click event? Then make an Ajax request to a PHP file or something?
Or, you could change your input type from "button" to "submit" <input type="submit" name="submitButton" id="submitButton" value="Submit" />
Then the...easiest (for lack of better words) is to check at the top of your page for the submit post variable. If it's there, PHP will handle it. Or, it will display the form if the variable is not set.
<?php
if (isset($_POST['submitButton']))
{
// form has been submitted.
// do something with PHP.
}
?>
<form action="file.php" method="post">
...
<input type="submit" name="submitButton" id="submitButton" value="Submit" />
</form>
I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.
On my form I have 3 submit,I want to know the best way to handle these buttons because the default action is the same form:
<form method="POST" action="file_where_form_is.php">
Name:<input type='text' name='name'>
<input type='submit' name='add' value='Add Names' ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit'name='Preview'value='Preview'ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit' name='submit'value='Submit' ONCLICK='window.location.href="send.php">
</form>
This not seems to work completely,just the 2 first work and for the last submit does nothing(with name='submit')
Any suggestions?
I finally found in header part add something like this:
if(isset($_POST['submit'])){
header("location: somefile.php");
}
elseif(isset($_POST['Preview'])){
header("location: anotherfile.php");
}
elseif(isset($_POST['add'])){
header("location: anotheronefile.php");
}