i have a question about formatting the URL parameters in php
page 1 can have these 2 url
http://somewhere.com/page1.php?foo=1
and
http://somewhere.com/page1.php
now on page 1, when i click a button, i want it to redirect to its self but with an additional parameters in the URL like so
http://somewhere.com/page1.php?foo=1&bar=2
or
http://somewhere.com/page1.php?bar=2
depending on the current url. How can i do this in php?
thanks,
Vidhu
First, check if a certain $_GET parameter is set, using isset.
If it is, echo a certain link. If not, echo a different link.
if( !isset($_GET['bar']) ){
echo 'link';
}
else{
echo 'link';
}
You can do a simple get system, So you will need to check if ?foo=1 is set if it is you can echo its content. If you want to show content for bar=2 check if its set and echo its content. This is just a simple way there more ways though.
The following form will submit to the same url with parameters added.
<form action="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="get">
<?php
// Iterate through our query string and add each key value pair as a hidden input
foreach ($_GET as $key => $value)
{
?>
<input type="hidden" name="<?php echo $key; ?>" value="<?php echo $value; ?>"/>
<?php
}
?>
<!-- new parameter to add -->
<input type="hidden" name="foo" value="1"/>
<input type="submit"/>
</form>
Simply you can do with $_SERVER['PHP_SELF']
For example
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="get">
<input type="hidden" name="foo" value="1"/>
<input type="submit"/>
</form>
Here the htmlentities() is used to avoid PHP_SELF exploitation as a security precaution
If you wish to avoid hidden input fields you can append the query parameters like action="<?php echo $_SERVER['PHP_SELF']; ?>?foo=1&bar=2"
I'm using these PHP functions to help me build urls correctly:
parse_str()
parse_url()
http_build_query()
http-build-url()
Example:
<?php
function add_parameters($parameters) {
parse_str($_SERVER['QUERY_STRING'], $old_parameters_as_array);
return $_SERVER['PHP_SELF'].'?'.http_build_query(array_merge($old_parameters_as_array, $parameters));
}
echo 'Link';
Related
I have a form like so:
<?php if (isset($_POST['artist'])) {
// do something
} ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
<p class="artist-negative">
<label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
</p>
<button type="submit">Update</button>
</form>
On the page in question, this form is shown many times in a foreach loop. However, when I submit any given form, it updates all of the forms, which is not what I want.
How can I append the $artist->ID to $_POST['artist'] so that I get something like:
$_POST['artist_1'] to match the checkbox attributes?
You could pair your foreach that generates the frontend form markup with a foreach that processes the form submission. Something like:
<?php
$regex = '/^artist_([0-9]+)$/'
foreach (array_keys($_POST) as $key) {
if (preg_match($regex,$key,$matches)) {
$artistId = (int)$matches[1];
// do something with $_POST[$key] according to $artistId
}
}
This works for a single field submission or a multiple field submission.
Alternatively, you could do something on the frontend in JS (as #smith suggests in the comments) to ensure the form submission always has the same, well-known keys, populating a hidden form with the current submission. With this approach you would have to add another field to the form that contains the ID.
The solution for this was much simpler than I was able to grasp at first, but basically I just had to do this, the key difference between this and my original question being the first two lines:
<?php $artist_form_id = 'artist_'.$artist->ID;
if (isset($_POST[$artist_form_id])) {
// do something
} ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
<p class="artist-negative">
<label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
</p>
<button type="submit">Update</button>
</form>
I'm working on a CakePHP project. I want to merge the values of $value into a string/array so I can later merge it with the string "TESTING" then implode the outcome to a single string without spaces!!!
<?php
$merge="TESTING"; ?>
<form method="post" name="payment_form" action="<?php echo $action; ?>">
<?php foreach ($fields as $name => $value): ?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
<?php endforeach; ?>
<p>some text<input type="submit" value="submit"/></p>
</form>
How can this be done? Thanks in advance.
EG: Please I want to keep the code as is. Meaning no for(var i=0;i++...) loops
Example
Let's say the values of the form are 1 2 3 productnr the outcome should be a string 123productnrTESTING no spaces just a string.
Well i wanted to send that data to a bank gateway to be processed. So it goes like this
1)Bank has sent me a string (The one called TESTING)
2)I print my form data (with the above code)
3) I need to send that data + the string provided into an imploded string to the bank gateway. Hope that clarifies some things :)
If I understand your question correctly, you would need to add a hidden field merge as a last one
The form page (View)
<?php
$merge="TESTING";
?>
<form method="post" name="payment_form" action="<?php echo $action; ?>">
<?php foreach ($fields as $name => $value): ?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
<?php endforeach; ?>
<input type="hidden" name="merge" value="<?php echo $merge; ?>
<p>some text<input type="submit" value="submit"/></p>
</form>
The action page (Action Controller)
If you are using CakePHP, you would need to get POST data from the Cake Request object.
$merge = implode($this->request->params);
echo $merge;
I want form to post automatically if zip variable is passed from URL.
URL looks like: www.sitename.com/maps/zipsearch.php?zip=90210
Form looks like:
<form method="post">
Zipcode:
<input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />
<input type="submit" name="subbut" value="Find instructors" />
</form>
So it fills the input box with zip code but I would like it to post automatically to see results again if zip is passed.
Maybe an IF / THEN?
Any help would be appreciated.
You mean to echo the value passed in GET parameter?
<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />
EDIT
Or, if you are asking about submitting the form, then something like this might work I believe:
<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />
<?php if( isset( $_GET['zip'] ) ) { ?>
<script>
document.forms["name_of_the_form_here"].submit();
</script>
<?php } ?>
like this:
<form id="form" action="form.php" method="post">
Zipcode:
<input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />
<input type="submit" name="subbut" value="Find instructors" />
</form>
<?php if (isset($_GET["zip"])): ?>
<script>document.getElementById('form').submit()</script>
<?php endif; ?>
since passing data via URL means GET method, so i think you have a little misconception with your question.
if you would like to post automatically you dont need to show form.
just put this code in your zipsearch.php
if ($_GET['zip'] != ""){
// do what you want if zip parameter is not null
}else{
// do what you want if zip parameter is null
}
It looks like your form is submitting to itself. (Eg. zipsearch.php displays HTML form. When user submits form, it is posted back to zipsearch.php which displays the search results).
If this is the case, you don't have to post anything, because you are already inside the file that handles the form submission. You could do something like this:
<?php
if (isset ($_POST['zip'])) {
$zip = $_POST['zip']; /* Form was submitted */
} else if (isset ($_GET['zip'])) {
$zip = $_GET['zip']; /* "?zip=" parameter exists */
}
if (isset ($zip)) {
/* Display search results */
} else {
/* Display form */
}
my page receives data which i retrieve with $_post. I display some data and at the bottom of page my button has to save data to mysql. I could submit form to next page, but how do i access the data that I have retrieved with post then? Lets say i have following code (in reality alot more variables ..):
<?php
$v= $_POST["something"];
echo $v;
echo "Is the following information correct? //this would be at the bottom of the page with the buttons
?>
<input type="button" value="submit data" name="addtosql">
You can do it in two methods:
1) You can save the POST variable in a hidden field.
<input type="hidden" name="somevalue" value="<?php if(isset($_POST["something"])) echo $_POST["something"];?>" >
The hidden value also will get passed to the action page on FORM submission. In that page you can access this value using
echo $_POST['somevalue'];
2) Use SESSION
You can store the value in SESSION and can access in any other page.
$v= $_POST["something"];
session_start();
$_SESSION['somevalue']=$v;
and in next page access SESSION variable using,
session_start();
if(isset($_SESSION['somevalue']))
echo $_SESSION['somevalue'];
Take a look. Below every thing should be on single php page
// first create a function
function getValue($key){
if(isset($_POST[$key]))
return $_POST[$key];
else
return "";
}
// process your form here
if(isset($_POST['first_name']){
// do your sql stuff here.
}
// now in html
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="first_name" value="<?php echo getValue("first_name"); ?>" />
<input type="submit" />
</form>
I'm sorry to repeat this question, but the thing is that I have done everything and nothing works. My problem is that I'm trying to pass variables to a second page and it won't work.
Page 1:
<form method="post" name="form1" id="form1" enctype="multipart/form-data" action="editempresas3.php?name=<?php echo $name;?>&descr=<?php echo $descr;?>&dir=<?php echo $dir;?>&pais=<?php echo $pais;?>&tel=<?php echo $tel;?>&fax=<?php echo $fax;?>&email=<?php echo $email;?>&url=<?php echo $url;?>">
<?php
$name = $_POST['empname'];
.....etc
?>
<input name="empname" type="text" required id="empname" form="form1">
.....etc
<input name="submit" type="submit" id="submit" form="form1" value="Crear">
Page 2:
The link will come without the variables
http://www.sample.org/editempresas3.php?name=&descr=&dir=&pais=&tel=&fax=&email=&url=
you should use GET method to achieve this.
change
<form method="post" name="form1" id="form1" enctype="multipart/form-data" action="editempresas3.php">
to
<form method="GET" name="form1" id="form1" enctype="multipart/form-data" action="editempresas3.php">
P.S: if you're form is not uploading anything you can't even miss enctype="multipart/form-data"
Possibilities, from most to least desirable:
Use sessions:
Page 1
session_start();
$_SESSION['var_for_other_page'] = 'foo';
Page 2
session_start();
$myvar = $_SESSION['var_for_other_page']
Use hidden fields:
<form action="secondpage.php" method="post>
<input type="hidden" name="var_for_other_page" value="foo" />
</form>
Put the get vars into the action URL:
<form action="secondpage.php?var_for_other_page=foo" method="post>
<input ... />
</form>
In this case you will have variables in both $_POST and $_GET.
Do not use either 2 or 3 to pass sensitive information.
If you want to send data from a form to a new page, firstly I think your should always use POST. The reason it is not working is you are attempting to send form data via POST but in your action you are trying to build a GET using PHP variables echoed in the there.
e.g.
action="editempresas3.php?name=<?php echo $name;?>&descr=<?php echo $descr;?>&dir=<?php echo $dir;?>&pais=<?php echo $pais;?>&tel=<?php echo $tel;?>&fax=<?php echo $fax;?>&email=<?php echo $email;?>&url=<?php echo $url;?>"
This can't work because PHP needs to process it before the HTML is rendered to print the variables you have chosen.
If you change your action to
action="editempresas3.php"
You will be successfully sent to the next page and if you then use
var_dump($_POST);
On your next page editempresas3.php you will get an output of all fields completed in the page 1 form.