How can I update a url variable value?
The url will look like that for example:
www.example.com/product.php?.........&page=1...........
I would like to change page value from 1 to 2, the reason that I typed dots instead of real url is since the url is dynamic and not static.
So how can I do that?how can I update and url variable value?
EDIT
I will refresh the page with the new value so the page will be re-filtered.
Can you try this,
$QUERY_STRING = $_SERVER['QUERY_STRING'];
echo $QUERY_STRING = str_replace('page=1', 'page=2', $QUERY_STRING);
PHP does not control your browser. The user and/or Javascript does. This means, you can not update a specific variable in the URL. (This variable is also called a HTTP GET Parameter).
What you have are below options:
Use an HTML form, with action="" & method=GET. Use hidden fields, with name=page and value=<number>.
<form action="" method="GET">
<input type="hidden" name="page" value="2" />
<!-- Use PHP to echo out all other GET parameters into hidden form fields -->
<input type="submit" value="click me to go to page 2"/>
</form>
Use Javascript: Parse out the GET parameters, and modify it.
Related
Is it possible for a user to enter a value into a form and then, on submit, have the page redirect to a new one with the value entered into the form stored in a PHP variable?
This if my form code;
<form id="loc-search" method="post">
<input type="text" id="search-by-location" name="custom-location" value="" placeholder="Sheffield, UK"/>
<input type="submit" id="submit" value=""/>
</form>
Once the user has entered a value in #search-by-location the page needs to redirect to weather.php with the value stored in a PHP variable called $location
AJAX / JS aren't my strong suits so if someone could point me in the right direction that would be great
Add the argument action="weather.php" to your form tag. Then, when clicked on the submit button, you will get redirected to that page. Depending on your method, in your case POST, the input values will be available in the superglobal $_POST array in PHP.
In your example, $location = $_POST["custom-location"]; will suffice. Note that the name, not the ID, determines the array key in the target PHP document.
Javascript or AJAX are not needed to achieve this.
This is just a normal form so why not just use $_POST after the redirect on the weather.php page:
$location = $_POST["custom-location"];
As #Tacticus pointed out you also need to have the form redirect (if you did not already do this in JS). By adding action="weather.php" in the form:
<form id="loc-search" method="post" action="weather.php" >
...
</form>
As stated in other answers you should modify your form to look like this:
<form id="loc-search" method="post" action="weather.php">
<input type="text" id="search-by-location" name="custom-location" value="" placeholder="Sheffield, UK"/>
<input type="submit" id="submit" value=""/>
</form>
In your weather.php file, you can get the value from the $_POST global variable, just like this:
<?php
$location = $_POST["custom-location"];
//Interpret data
?>
Note that you can access the value of an input tag from your form witch was passed, with the input's name. In html you specify the following:
<input name="yourname" />
And when you want to access that value, you simply refer to his name.
$_POST['yourname']
If you use GET method for the form to pass the values, then you do the same, only the value will be stored in the $_GET global variable, so in your case with a GET method the variable initialization would look like this:
<?php
$location = $_GET["custom-location"];
//Interpret data
?>
I'm having a problem with my HTML GET form that's connected to a PHP script, so, basically, when the action is done I see the SUBMIT button value in the URL, so it's like this http://url.com/?valueI=Want&submit=Submit+Value.
How do I stop that from happening?
Remove the name attribute from the submit element to prevent it from being passed in the query parameters.
See: Stop the 'submit' button value from being passed via GET?
This is the nature of GET requests. The submitted values, aka Query String, are shown as part of the URL after a ? suffixing the page URL.
If you don't want it to show up, use POST method, or make a script that submits using Ajax.
Now if the question is only about the text in the submit button being shown, if you don't want it to get submitted along with the rest of the form all you have to do is not give it a name.
<input type="submit" value="Send Form">
No name="..." in the tag.
you need to set the form method
<form action"/your/path" method="post">
...
</form>
You can use button tag to submit the value using GET method.
<button type="submit">Submit</button>
do something like:
<form action="myfile.php" method="get">
(your form elements here)
<input type="submit" value="Submit" />
</form>
When I use <form action="code.php?id=1" method="post"></form>, the form id is passed in the URL. But when I write the same code by replacing 'POST' by 'GET', the id is not passed to the URL.
Why?
When you submit a GET form, the values from the form are appended to the action URL, as the "query string" after the ?. Specifying an existing query string in the action attribute of such a form creates an ambiguity. Browsers don't merge the two query strings, they just throw away the old query string and build the new one based on the form.
With a POST form, there is no ambiguity: the data from the form is sent separately from the URL, so there is no need for the query string to be over-written.
However, it's probably best not to mix the two kind of parameters, so the solution is always to include your extra parameters as hidden fields, then it will work with both GET and POST forms:
<input type="hidden" name="id" value="1">
Better way is to pass id in hidden field.
<form action="code.php" method="post">
<input type="hidden" value="1" name="id" />
</form>
If your form is as below
<form action="code.php?id=1" method="post">
<input typ"text" name="username" />
<input type="submit" />
</form>
example script in code.php
<?php
print_r($_GET);
print_r($_POST);
print_r($_REQUEST);
?>
You will get form data in post array and url parameters in get array, in request you will get both get and post data in one array. But if you change from post to get method your form data added with the url instead of appending. This issue is because of ambiguity. To get solution i this situation, create a hidden field in your form those you also want to send with query string.
I want to pass more than one variable in the URL bar using PHP, however I want one of the variables to be added onto another existing variable in the URL bar.
For example:
Let's say you have: test.php?u=2 and let's suppose one of the fields is age. Once I click submit I want the URL to look like test.php?u=2&age=22 but on the same page.
How could I do this? Would I have to redirect the user again?
<form method="get" action="?"> - use this as your form tag. It'll submit to the same page, with all the form values in the querystring, accessible via $_GET.
You can write code that injects all request variables into the form that the button submits. For example:
<form ... >
<?php foreach($_REQUEST as $key => $value) {
echo sprintf('<input type="hidden" name="%s" value="%s" />',
htmlspecialchars($key),
htmlspecialchars($value));
}
?>
<!-- the one you want to add follows -->
<input type="hidden" name="age" value="22" />
<input type="submit" />
</form>
It isn't pretty, but it works (if you don't mind that the vars from either $_GET or $_POST will actually end up being submitted with other method, whichever one it is the form uses).
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