Pass a post / get Variable using onclick in a div - php

I am trying to pass a variable (maybe the name of the div), to create an dynamic page without using javascript.
I try to avoid javascript as much as possible, as you could imagine from the first sentence.
My script looks kinda like this:
<?php $topbar = $_GET['topbar']; ?>
<form action="" method="get">
<div id="navigationTopContent" name="start" onclick="window.location=''">
1. entry
</div>
<div id="navigationTopContent" name="group" onclick="window.location=''">
2. entry
</div>
</form>
but it is absolutely not working.

you are already using form to post/get values, just use input type hidden if you want to sent data anonymously.
<input type="hidden" name="abc" value="data">
when using form you must use submit button to post data.
or you can use anchor tag to post data as :
echo 'Entry';

If you are trying to pass a post / get variable, apprently you have to use buttons and correct the css so that the button is looking like your div was. You them the same name and different values. It looks kinds like this:
<form action="" method="get">
<div id="navigationTop" class="text">
<button class="navigationTopContent" name="topnav" value="start">Start</button>
<button class="navigationTopContent" name="topnav" value="group">Groups</button>
</div>
</form>
<?php $topnav = $_GET['topnav']; echo $topnav; ?>
this is not perfect, since the buttons is, due to some magic always a bit darker, but ok.

Related

GET parameter won't pass with html button formaction

I am trying to pass a GET parameter through a button but I can't figure out what I am doing wrong. The parameter is set, as it shows up fine in the header, but it isn't being added to the edit.php url. The button is directing me to edit.php, just without the GET parameter added. I am pretty new to this stuff and this is my first time using links that aren't through anchor tags, so I am clearly missing something here. Any advice is greatly appreciated.
<h1 class="headerWithButton">Claim #<?echo($_GET['claim_id'])?>
<form>
<button type="submit" formaction="index.php" class="backButton">Back</button>
<?echo('<button type="submit" formaction="edit.php?claim_id='.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
</h1>
When you submit a form using the GET method, any existing query string in the action will be replaced by a new one generated by the name and value of the successful controls associated with that form.
In your case, the only successful control is the submit button, which doesn't have a name or a value.
You could get the effect you desire by moving the data to those attributes:
<h1 class="headerWithButton">Claim #<?php echo htmlspecialchars($_GET['claim_id']); ?>
<form>
<button formaction="index.php" class="backButton">Back</button>
<button formaction="edit.php" name="claim_id" value="<?php echo htmlspecialchars($_GET['claim_id']); ?>" class="editButton">Edit</button>
</form>
</h1>
Important security note: inserting data from the URL directly into a page makes you highly vulnerable to XSS attacks. You need to take precautions against that. The most basic of those is using htmlspecialchars.
Note, however, that it isn't really appropriate to use a form here. Your form buttons are not submitting any data the user has entered, nor performing any kind of action. The affordances offered by buttons are misleading here.
You can, and should, use regular links instead.
<form method="get" action="edit.php">
<?echo('<button type="submit" formaction="edit.php?claim_id='.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
instead of using the form you can just use a straightforward link
k with the anchor tag
edit
or you can specify the methos of get on the form with a hidden for input to place the link get parameter
If you have to use formaction, you must specify name and value of element:
<h1 class="headerWithButton">Claim #<? echo($_GET['claim_id'])?>
<form>
<button type="submit" formaction="index.php" class="backButton">Back</button>
<?php echo('<button type="submit" formaction="/edit.php" name="claim_id" value="'.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
here it is better to place buttons in different blocks. But personally, in this case, I use a hyperlink
<form method="get" action=""></form>
<?echo('Edit
I find the easiest way to pass values with a button is to just wrap a form around the button.
$id = $_GET['claim_id'];
echo <<<EOT
<form action="index.php" method="get">
<button>Back</button>
</form>
<form action="edit.php" method="get">
<button name="claim_id" value="$id">Edit</button>
</form>
EOT;

I want to save the form info on a div so that it stays on the page when its refreshed

I want to save the form data, that's transformed to a php (I already got that part, its designed and styled in css) I want to save it so that the form is available on a page for everyone to see, so that when I refresh the page, the div that has the form info stays there.
<form name = "quoted" form action="genericwebpage.html" method="post">
<input id = "poster" type="text" name="poster" placeholder = "Credited Individual."> <br>
<textarea class = "actual-quote" name = "actual-quote"placeholder = "Write the quote here!"></textarea><br><br>
<input id = "submit1" type="submit">
</form>
and here's the php found on the webpage it's going to.
<div class="wrapper">
<div class="submissions">
<div class="logo-logo"><h2>Generic.</h2></div>
<div class="top-submit"><?php echo $_GET['actual-quote']?></div>
<div class="poster"><?php echo $_GET['poster']?></div>
</div>
Any suggestions, is EXTREMELY welcomed.
Thanks!
-Connor
First, you need to change $_GET to $_POST because your form has a post method. That should make it so that when the form is submitted, the data will be displayed in the div where you have it being echoed. However, that will only be temporary ... it won't be anywhere for "everyone to see" unless you save it in a database.
You hav to use Local Storage or Server Side Scripting (eg PHP).

HTML form to GET two variables from a PHP page

I've been working for several hours trying to get this to work properly. The page I have a form on is /index.php?action=pagename. I have a form that needs to get a variable from the following /index.php?action=pagename&thing=something. My HTML form going like this:
<form role="form" action="pagename" method="get">
<input type="text" name="thing">
</form>
This form is located on /index.php?action=pagename and I want to get &thing from that URL.
Any ideas?
The problem I'm having is that when the form is submitted, the URL redirects to index.php?thing instead of staying on index.php?action=pagename.
It looks like you might be having some trouble with <forms> in general and not just PHP so here is an overview:
<!-- the action is where you want to send the form data -->
<!-- assuming THIS code is the index.php file then we want to send the data to ourselves -->
<!-- the method is GET so it will be directly accessible from the URL later -->
<form action="index.php" method="GET">
<!-- add a hidden value for pagename -->
<input type="hidden" name="action" value="pagename">
<!-- the name called "thing" will be appended to the URL and it's value as well -->
<input type="text" name="thing" value="<?php echo (isset($_GET['thing']) ? $_GET['thing'] : ''); ?>">
<br>
<!-- click this button to submit the form to itself -->
<!-- once this has been submitted then you can retrieve the URL value with $_GET as you can see above -->
<input type="submit" value="submit" />
</form>
You can simply use the PHP-variable $_GET['thing'] to get the value.
The action attribute of the form element is the target script which will be called on submit. If blank it will be the current script which is showing the form. You also can only give url parameters beginning with ?.
<form role="form" action="?action=pagename" method="get">
<input type="text" name="thing">
</form>
Exerpt from php.net: http://www.php.net/manual/de/tutorial.forms.php
One of the most powerful features of PHP is the way it handles HTML
forms. The basic concept that is important to understand is that any
form element will automatically be available to your PHP scripts.
Please read the manual section on Variables from external sources for
more information and examples on using forms with PHP. Here is an
example HTML form:
Example #1 A simple HTML form
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
There is nothing special about this form. It is a straight HTML form
with no special tags of any kind. When the user fills in this form and
hits the submit button, the action.php page is called. In this file
you would write something like this:
Example #2 Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
A sample output of this script may be:
Hi Joe. You are 22 years old.
Basic script I made to get the variables from the URL:
<?php
if (isset($_GET["action"]) && isset($_GET["thing"])) {
$action = $_GET["action"];
$thing = $_GET["thing"];
echo $action .PHP_EOL . $thing;
}
?>
This will output the following for the URL /index.php?action=pagename&thing=something
pagename
something
Though you should probably learn how to use forms properly first.

Use PHP to pull values from HTML forms and store them in MySQL DB

I seem to have a bit of a problem here that I can't quite figure out.
So the deal is I have a php script along with an HTML file, in that file I have a few forms with some text boxes and some drop downs. Now the reason I had to go single forms on all of these was because if I did one big one none of them would work when I would hit the submit button. I have no idea... My current code is below, my previous code only had one form and it wouldn't work at all.
<form method="post" id="locationFromPost" name="locationFormPost">
<div class="formRow">
<div class="label">
<label for="locationForm">Current Location:</label>
</div>
<div class="field">
//If post is null go back to value pull originally else echo post
<?php if($_POST['locationForm']==null) $location=$location;else $location=$_POST['locationForm']; ?>
<input type="text" name="locationForm" id="locationForm" value="<?php echo $location?>"></input>
</div>
</div>
</form>
...... and so on.......
<form method="post" id="sexFromPost" name="sexFormPost">
<div class="formRow">
<div class="label">
<label for="sexForm">Sex</label>
</div>
<div class="dropDown">
<select name="sex" id="sexForm" >
//If post is null go back to value pull originally else echo post
<?php if($_POST['sexForm']==null) $sex=$sex;else $sex=$_POST['sexForm']; ?>
<option value="1" <?php if($sex==1){echo"selected='selected'";}?>>Male</option>
<option value="2" <?php if($sex==2){echo"selected='selected'";}?>>Female</option>
<option value="3" <?php if($sex==3){echo"selected='selected'";}?>>Not Specified</option>
</select>
</div>
</div>
</form>
...... and so on.......
<form method="post" id="submit" name="submit">
<div class="formRow"><div id="seperator"></div></div>
<div class="submitButton">
<input type="submit" name="submit" id="submit" value="Save Changes"/>
</div>
</div>
</form>
So thats what the top part of my code looks like, all the div's are for formatting you don't have to worry about those. Now the php code to save the information is below. I should note that the variables you see in the php parts of the code above are retrieved from the Database in the earlier part of the code, thats working fine. What I am having problems with is I want a user to be able to edit their information then just hit the submit button and the information to be saved. What currently happens is, well, absolutely noting. The page will refresh and all the values will go back to what they were when they were pulled, they are not stored in the DB at all, now I checked to make for sure that part of the code works, buy doing some test. It saves the data no problem. What I thin is the problem is the forms them-selfs, if I do everything in one big form it doesn't work... Don't know why. now if I hit enter at the end of each of the forms individually in the browser the data goes in but just that field, sometimes other fields are wiped completely out and a null or empty string is stored. I cant seem to figure this thing out at all.
heres the php to save the information.
<?php
if (isset($_POST['submit']))
{
mysql_query("UPDATE members SET location= '".$_POST['locationForm']."' WHERE usr='" .mysql_real_escape_string($_SESSION['usr']) ."'");
mysql_query("UPDATE members SET location= '".$_POST['sexForm']."' WHERE usr='" .mysql_real_escape_string($_SESSION['usr']) ."'");
........ you know the rest.......
}?>
I am completely lost at why this thing is doing this, now im not the best PHP programmer by any-means, so it could (probably is) be me. If anyone could point out what I would need to do to get a system like that working please let me know
Thanks.
I wasn't clear exactly what your problem was.
However this:
<?php if($_POST['locationForm']==null) $location=$location;else $location=$_POST['locationForm']; ?>
Doesn't look to me like it is going to do much. Try something like this:
<?php
if(!isset($_POST['locationForm'])){
$location=$location;
}
else {
$location=$_POST['locationForm'];
}
?>
(Curly braces are my own personal liking)
Assuming you have $location defined somewhere you didn't show us. This will check if there is NOT a $_POST['locationForm'], and if there is it will use it.
Try placing all the form fields within on form tag and also use mysql_real_escape_string on your variable as well.
you need all the fields that are going to be submitted to any given page to exist in a single form tag. any elements in a different form tag will submit as a set of THAT form tag.
I' assuming that you also want to maybe combine down your updates?
//$_SESSION['usr'] should already be sanitized and safed wherever you handle your site credentials..
//set your submittable values, with a default to an empty string in this case.
$sex=isset($_POST['sexForm'])?mysql_real_escape_string($_POST['sexForm']):"";
$location=isset($_POST['locationForm'])?mysql_real_escape_string($_POST['locationForm']):"";
//single statement, no need to iterate for each field, that's wasted connections.
$sql="update "UPDATE members SET location= '$location' , sex='$sex' WHERE usr='".$_SESSION['usr']."'";

Can php a href post not use a link like index.php?id=value

I want to post some value through a href. However, I do not like to use a link like index.php?id=value. Is there any other method?
I want to post test1 and test2.
<div id="div1">
<li>test1</li>
<li>test2</li>
</div>
<div id="div2">this is a <? $_POST["name"]; ?>, just a test.</div>
Use a form, with method=POST, and a hidden form field for the value you want to post. The 'href' either becomes a submit button, or have an onclick action on the link that just submits the form.
You can apparently use PHP for this, and the information is supposed to be here, but it is currently not working:
http://www.zend.com/zend/spotlight/mimocsumissions.php
Otherwise, you should probably just do this:
<form id="myForm" method="post" action="index.php">
<input type="hidden" name="id" value="value">
<input type="hidden" name="somethingElse" value="test">
<!-- more stuff you want to post -->
</form>
Post!
The form is invisible, but you need one form per link like this (or I guess you could use only 1 form total and call another javascript function from the onclick event that modifies the values of the form fields). I don't really see the benefit though.
You could use either jQuery/ajax to post your data to a backend or post the variables as said in the answer above.
You can use mod_rewrite (if you are using Apache) to modify the look of url. e.g. instead of www.test.com/index.php?id=value you can use www.test.com/value or www.test.com/index/value

Categories