I understand how to update active user data via using a form... But how to i update a certain column for the currently active user by click a button NO FORMS for example... i have a main page where a user can login via facebook, well when they login and accept permissions it then forwards them to the next step of my application, when they successfully authenticate all there information is stored onto my local mysql database in a users table. now they see the next page which consist of two buttons. Now what i want is if they click the "doctor" button it updates the column "job" to say doctor, and visa versa if they click "programmer" it updates the "job" column to programmer.
I'm trying to totally avoid letting them fill out information, I just want it to be a click this button append this to that field or click this button append that. If I'm not making any since, let me know and I will gladly explain in further details.
Do you mean something like this?
Update
Your php file:
<?php
if($_POST['Doctor']){
$sql =("UPDATE users SET job='Doctor' WHERE userid='$someactiveuserid'");
$result = mysql_query($sql) or die(mysql_error());
} elseif($_POST['Programmer']){
$sql =("UPDATE users SET job='Programmer' WHERE userid='$someactiveuserid'");
$result = mysql_query($sql) or die(mysql_error());
}
?>
<html>
<body>
<form method="post" name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="Programmer" value="Programmer"></br>
<input type="submit" name="Doctor" value="Doctor">
</form>
</body>
</html>
Hope this helps!
Related
I am currently working on a html/script that uses google's image search to simply return a yes or no if results are found, I think I can handle parsing the information from the user (URL of an image) into the google image search URL string but I can't determine what variable or how I could get my script to recognize if results were found or not... I apologize for my script below as the only language I've used before is ASP but have very little experience:
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Link to image:<br>
<input name="name"/><br>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
if (($name==""))
{
echo "Please Enter a URL";
}
else{
echo "https://www.google.ca/searchbyimage?site=search&image_url=$name&as_sitesearch=jamesmorgan.ca&safe=images";
}
}
?>
I'm also aware that my secondary echo commands does nothing, I was thinking maybe I could use the header command to just redirect them to the google image website with the results but ideally I'd like the script to recognize whether or not there are results, I feel like I might be approaching this all wrong, thanks in advance.
Well, for starter, you might want to check if $_REQUEST['action'] is set in line 1 first, such as isset() function.
As for handling the feedback, you may try looking into PHP Simple HTML DOM Parser - which basically allows you to get the HTML content from a specific page, and you can process it yourself and etc.
Hope this helps! Good Luck!
When I was learning Php with Mysql, I got a problem :
<form action="?" method="POST">
.
.
.
</form>
Why do we put "?" in the action attribute?
PS: this form was for inserting text into a database.
Actually, I have an Index.php file which is the controller, and 2 files (form.html.php and joke.html.php as templates). In the tutorial, I first clicked on a link "add joke" to include the form.html.php file and in this form there is <form action="?"> </form> and a submit <input>. When I click on submit, the controller index tests and executes the inserted SQL query.
Thanks.
Personally don't ever do that.... Use action="action.php" or use action="" post to the current URL.
Not sure what you are trying to accomplish with ? in the action attribute.
"We" don't and "You" shouldn't do so either.
If you want to POST the data to the current URL, leave it empty, i.e. action="".
If you want to POST to another URL, put that URL in there, e.g. action="save.php".
I have a simple application form it is divided in 3 pages. What I want is when i fill up the 1st page, then when i go to another page the data is still there so I co go back to first page when I want to changes the data I inputted.
I tried saving it in session but I think that is not the right way to do it. any suggestions?
As you post the page, set the values you post in session variables
e.g.
<?php $_SESSION['name'] = $_POST['name']; ?>
Then in your input fields, check to see if the session is set & if so they display that.
e.g.
<input type="text" name="name" value="<?php if (isset($_SESSION['name'])) { echo $_SESSION['name']; } " />
Make sure you start a session on each page you want to use them on!
<?php start_session; ?>
Im currently creating a website for my chickens that will have an "egg counter". I have a hidden part of the site that is password protected where I want to put a button that every time I press it, adds one to the egg value. Currently, I have an html file that has just the number in it which I have embedded into the site.
(this is the code that embeds the .html file)
<h2><b>Egg Counter!<b/></h2>
<p>So far our chickens have laid</p>
<font size="20" color="#FFFFFF"><embed src="./eggs.html"></embed></font>
I'm a novice programmer and I'm not sure whether this is the right approach but I have no idea how to make the button that changes the egg value.
Any help is greatly appreciated!
You can use the javascript onclick event to increment the number by 1 per click.
place this in your head section.
function incrementValue()
{
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('number').value = value;
}
Place this where you want the button to display.
<form>
<input type="text" id="number" value="0"/>
<input type="button" onclick="incrementValue()" value="Increment Value" />
</form>
I think you want the number to be stored, so it's always showing and you can update it as and when your hipster chickens lay eggs. The previous answer will allow you to increment the number but only while you have the browser open.
I think the best way to do this is with PHP.
So first of all you want your page to be .php , lets say admin/loggedin.php.
You want to create a blank file in the same directory called eggs.txt , with the permissions 0755.
Then you want your form to look like this
<form method="POST" action="loggedin.php">
<input type="text" name="num" value="0"/>
<input type="submit" value="Chicken came first" name="eggs">
</form>
To explain the method / action:
method
is how you want to send the data to the
action
page.
Options are $_POST and $_GET, I don't think going into too much detail here will help.
action= is to tell the form what page to send this data to, in this case we've chosen to send it to the same page the form is on (loggedin.php).
So now once you submit that form we will have a $_POST['num'] available to us which contains the number you entered into the form input field.
To get this, you want to add at the top of your page: EDIT (have changed the section of code below - file_put_contents should of been inside the if($_POST['eggs']) {
<?php
//this checks to see if the form was submitted by checking 'eggs' is set.
if($_POST['eggs']) {
// create a nicer variable for our egg number, it deserves a nice name.
$egg = $_POST['num'];
//this will put the new number of eggs in our eggs.txt file
file_put_contents('eggs.txt',$egg);
}
?>
file_put_contents() writes to a file
Next you want to be able to get that number from the file on your display page. To do this:
<div class="my_egg_count">
<?php echo file_get_contents('eggs.txt'); ?>
</div>
file_get_contents() reads a file, echo 'echo's' from the server to client side.
Let me know if you have trouble with this, you don't want to end up with egg on your face.
EDIT:: try this
if(is_writeable('eggs.txt')) {
if(file_put_contents('eggs.txt',$egg)) {
echo 'File Updated';
}else {
echo 'Error Updating File';
}
}else {
echo "Error: File Isn't Writeable [possibly named incorrectly, isn't in the correct location, or wrong permissions]";
}
I'm using Loopj's TokenInput and it's working fine.
I have three search boxes on the same page - one each for three different search attributes and each with its own external data source (in this case, search by ship name, ship class and ship type). Of course, there are three 'Submit' buttons, one for each search box.
My Problem: Clicking any 'Submit' button only returns values for its own search box (based on included script-refer below). What I'd like is to click ANY button and get the values for ALL the search boxes so that I can create a MySQL query.
<script type="text/javascript">
$(document).ready(function() {
$("input[type=button]").click(function () {
alert("Would submit: " + $(this).siblings("input[type=text]").val());
});
});
</script>
Note: This earlier question "Using tokeninput jquery plugin on more than one input on a page".seems related but the answers to that question didn't address this issue.
After some further testing, I've figured out the answer and I'll include it here for the sake of completeness. Unfortunately this highlights just how little I know about coding...
The javascript 'alert' script is a red-herring. It doesn't magically get the token values out of thin air. The token values are already stored in the relevant input fields.
The "Submit" buttons are a red-herring. That's because they are
". They're just there to trigger the
javascript alert, they can't submit a form.
You need a form! The "TokenInput" demos show how the plugin works, but they're meant to be used IN A FORM.
I added a form field at the top of my page and a closing form field (</form>) at the bottom of the page. The result was that my three search boxes were inside the form. Note the action is to retrieve the same page and method = post.
`<form id="myshiptype" name="pixsearchform" action="<?php echo $_SERVER[REQUEST_URI]; ?>" method="post" >`
I changed the sample input "name" fields from 'blah' to something more meaningful - shipid, classid and typeid.
I added a Submit button (of the <input type="Submit" /> variety) to the bottom of the form. <input type="submit" id="update" name="update" value="Update search" />
I added some debugging code to the page to show the values of the input fields after the Submit button is clicked.
<?php
$postvalues = "";
if ($_POST) {
$kv = array();
foreach ($_POST as $key => $value) {
$kv[] = "$key=$value";
}
$postvalues = join("&", $kv);
}
echo "post values are ".$postvalues;
?>
Here's a sample of the debug - "post values are shipid=34&classid=&typeid=677,638&update=Update search"
Next steps are to adapt the form to Ajax and also disable the "Enter" key in the form (for when a user hits Enter in an empty Search).