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>
Related
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']))
{
//...
}
}
I want to pass data from a single form, but it has another 2 button to add and delete questions, the problem is when I click on any of problem or create a question problem, the form problem to the action url.. I just want when I click on the Submit button then action is activated.
<form method='POST' action='scripts/generateguide.php'>
<button class='deleteCon'>Delete</button></p>
<button class='addP'>create quesiotn</button>
<input name='createGuide' type="submit" value="Submit">
</form>
The default type of a button element is submit.
You can specify type="button".
It is better practise, however, to give it a name and a value so that you can perform whatever task you want it to do server side should the JS fail, and then call event.preventDefault() in the event handler function.
I think I have found the solution instead of using
<form method='POST' action='scripts/generateguide.php'>
<button class='deleteCon'>Delete</button></p>
<button class='addP'>create quesiotn</button>
<input name='createGuide' type="submit" value="Submit">
</form>
I have to use this
<form method='POST' action='scripts/generateguide.php'>
<input name='create question' class='addP' type="button" value="Submit">
<input name='delete' type="submit" value="btton" class='deleteCon'>
<input name='createGuide' type="submit" 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.
I'm wondering how to resolve an issue where I have one text box and two buttons.
Each button needs the same data in the text box to accomplish its task.
One button is to update the existing record they are reviewing (with the new value in the text box), and the other button is used to add a new record (again, using the new value in the text).
One idea I had was to use jquery to update a hidden text box that gets updated when the visible text box is modified by the user.
So something like this: (this is just pseudocode...)
<form name="form1" method="post" action="controller1/method1">
<input type=text name=visibleTextBoxForForm1></input>
<button type=submit value=UPdate>
</form>
<form name="form2" method="post" action="controller2/method2">
<input type=hidden name=hiddenTextBoxforForm2></input>
<button type=submit value=New>
</form>
<script>
$('#visibleTextBoxForForm1').live('change', function() {
//update a hidden textbox in form2 with value of this textbox.
});
</script>
Is there a better way to do this?
Alternatively, you could do it via JQuery. Tie a clicklistener for each button and provide the correct URL to the form on click.
Here's some quick code... you'd have to correct the proper jquery queries for the correct elements.
<form name="form1" method="post">
<input type=text name=visibleTextBoxForForm1></input>
<button type=button value=Update>
<button type=button value=New>
</form>
<script>
$('update').click(function() {
$(form1).attr('action', <update url>).submit();
});
$('new').click(function() {
$(form1).attr('action', <new url>).submit();
});
</script>
If that's the only field, then simply have one form with two buttons and handle that text data based on the name of the button used to submit it.
<form name="form1" method="post" action="controller1/method1">
<input type="text" name="text" />
<input type="submit" name="insert" value="Insert New Data" />
<input type="submit" name="update" value="Update Existing Data" />
</form>
PHP (not CodeIgniter, since I'm not familiar with that framework):
if(isset($_POST['insert'])) {
// insert $_POST['text']
} else if (isset($_POST['update'])) {
// update $_POST['text']
} else {
// error
}
I am trying to create a button that is a link which also passes variable information, but I see that it is not being passed in the url, so it i snot working. Here are the attempts I have made:
<form><input type="button" value="link 1" onClick="myaccount.php?userinfo=0'"></form>
<FORM METHOD='LINK' ACTION='myaccount.php?userinfo=0'>
<INPUT TYPE='submit' VALUE='Details'>
</FORM>
If anyone can show me where i am going wrong that would be appreciated.
You needs to add location.href
onClick="location.href = 'myaccount.php?userinfo=0'">
<FORM METHOD='GET' ACTION='myaccount.php'>
<input type=hidden name=userinfo value=0>
<INPUT TYPE='submit' VALUE='Submit'>
</FORM>
There is no method="link" only GET, POST, PUT, DELETE and some others. The action attribute of a form specifies where to send the data, while the method specifies how to do so. GET, for example, will append the variables and their values to the URL.
I'm guessing something like this would work:
<form action="myaccount.php?userinfo=0" method="get">
<button type="submit" name="link" value="1">Details</button>
<form>