I have strange problem with my search form. After I enter keyword and do the search request I get empty parameter value.
For example I type in the search field the word "something"
I see an empty value:
search.php?keyword=
After this I enter the keyword "else" and I recieve:
search.php?keyword=something instead of search.php?keyword=else
They somehow appear with "one step back"
I was trying to debug with print_r and var_dump but I only can print some values that does not explain my problem.
Am I missing something very trivial?
Here is what I have:
My class function:
public function show_search_result() {
$this->search_keywords = strip_tags($_GET['keyword']);
$this->_db->query("SELECT * from posts WHERE post_title LIKE '%$this->search_keywords%' OR post_content LIKE '%$this->search_keywords%' LIMIT 100");
$this->rows_results_found = $this->_db->resultset();
}
And my form:
<form action="search.php?keyword=<?php
if (isset($search_results->rows_results_found) && isset($_POST['search_requested'])) {
print strip_tags($_POST['search_keywords']);
}
?>" method="post">
<input type="hidden" name="search_requested">
<input type="text" name="search_keywords" value="<?php
if (isset($search_results->rows_results_found) && isset($_POST['search_requested'])) {
print strip_tags($_POST['search_keywords']);
}
?>"><input type="submit" value="Search">
</form>
<form action="" method=get>
<input type=text id=se>
<?php
if($_GET != null){
$sekw = $_GET ['se'];
$sql = //the query like='$sekw' limit=100;
?>
<input type=submit>
</form>
A simple code.
Your problem is when you send the form, it does save the keywords until the second send.
change your method from post to get. also i would advice you to use a framework for easy and fast coding. some include symfony2, laravel
Related
So the issue I'm running into is I have a project with nested forms to select options and for some reason I cant get it to get beyond the first form. If you run this and select the first button it correctly displays the second button, but after that it just returns to the beginning.
How do I do this correctly? I've tried various methods such as isset, using functions, wiping the $_POST variable, etc and to no avail. Would Google or Stackoverflow this but I'm not quite sure what this problem is called.
This is all being done within a single php file because I don't want to have to deal with leaving the page, and this started out as a simple assignment that I've greatly expanded to fit my needs. Also I know nothing of Javascript and have no interest in using it.
<html>
<body>
<?php
echo <<< HERE
<form method = "post">
<input type = "submit" name = 'button' value = 'Do thing 1'>
<br>
</form>
HERE;
$button = $_POST['button'];
if ($button == 'Do thing 1'){
echo <<< HERE
<br>
<form method = "post">
<input type = "submit" name = 'button2' value = 'Do another thing'>
</form>
HERE;
$button2 = $_POST['button2'];
if ($button2 == 'Do another thing'){
echo 'doing another thing';
}
}
?>
</body>
</html>
The way I would solve this kind of issue is by sending all the fields and naming the buttons:
<form>
<input name="input_1">
<input name="input_2">
<button type="submit" name="button" value="button">Button</button>
<input name="input_n">
<button type="submit" name="button" value="button2">Button2</button>
</form>
Then once submitted:
if($_POST['button'] == 'button') {
/* sanitize input 1*/
/* sanitize input 2*/
/* do something */
}
if($_POST['button'] == 'button2') {
/* sanitize what you need */
/* sanitize input n*/
/* do something */
}
I'm a newbie in PHP, and I would like to send datas from a form and display it into the same page, here is my code for better understanding:
<form method="post" action="same_page.php">
<input type="text" name="owner" />
<input type="submit" value="Validate" />
</form>
<?php
if(isset($_GET['owner']))
{
echo "data sent !";
}
?>
So normally, after having entered some random text in the form and click "validate", the message "data sent!" Should be displayed on the page. I guess I missed something, but I can't figure out what.
You forgot to add submit name in your form.You are using POST as method so code should be
<form method="post" action="">
<input type="text" name="owner" />
<input type="submit" name="submit_value" value="Validate" />
</form>
<?php
if(isset($_POST['submit_value']))
{
echo '<pre>';
print_r($_POST);
}
?>
Will display your post values
You are using a POST method in your form.
<form method="post" action="same_page.php">
So, change your code to:
if (count($_POST) && isset($_POST['owner']))
Technically, the above code does the following:
First checks if there are content in POST.
Then, it checks if the owner is set.
If both the conditions are satisfied, it displays the message.
You can actually get rid of action="same_page.php" as if you omit it, you will post to the same page.
Note: This is a worst method of programming, which you need to change.
You should Replace $_GET['owner'] with $_POST['owner'] as in your form you have specified method='post'
Replace:
$_GET['owner']
With:
$_POST['owner']
Since you are using the post method in your form, you have to check against the $_POST array in your PHP code.
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 */
}
I am trying to write a dynamic form using PHP. I'd like to have a single webpage that contains two forms:
The upper form allows to search for an element in the mysql database, e.g., for a name
The lower form shows the data that is associated with this name in the database
If I press on the "Search" button of the upper form, then the the lower form is shown and the text fields are filled with data from the database that belong to this name. If I change the user name to some other value and press again "Search", then the data that is associated with the new record is shown and so on.
The lower form also has a button "Update" which allows to transfer changes made to the text boxes (in the lower part) to the database.
Now, I have the following problem: In my script I set initially the value of name (from the upper form) to "". When I then press the "Search" button, then the lower part of the form is shown and the corresponding data is shown in the lower part. When I then press the "Update" button, then the text field associated with name is set to the empty string. This is because in my script I set initially name to the "". I'd like that in this case the data entered in the upper form is not changed, i.e., it stays the same.
I guess, I am missing something here. There is probably an easy solution for this and I am doing something fundamentally wrong. It'd be great if you could help me.
That's what I tried... I deleted lots of details, but I guess that can give you an idea what I am trying to do. Notice that the whole code is in the file update.php.
<?php
function search_bus($mysql, $name)
{
// do some stuff here...
}
function update_bus($mysql, $b_id)
{
// do some stuff here...
}
// some global variables
$b_id = 0;
$username = ""; // username of business
// get b_id that corresponds to username
if (isset($_REQUEST['search']))
{
$b_id =0; // business id
if (isset($_POST['user']))
{
$username = $_POST['user'];
$b_id = search_bus($mysql, $username);
}
}
elseif(isset($_REQUEST['update']))
{
update_bus($mysql, $b_id);
}
?>
<h2>Search:</h2>
<form name="search_bus" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="submit" value="Suchen" name="search"/>
</form>
<?php
if($b_id != 0)
{
?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<-- some form follows here -->
<?php
}
?>
I think what you're missing is to create a HTML Hidden field to keep the value of Name variable.
<input type="hidden" name="name" value="<?php print $nameVar ?>" />
Add this input to both forms so you can keep the value no matter what button the user clicks.
Hope this helps.
Adding code to verify the
<h2>Search:</h2>
<form name="search_bus" method="post"
action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<input type="submit" value="Suchen" name="search"/>
</form>
<?php if($b_id != 0) { ?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];>">
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<-- some form follows here -->
<?php } ?>
Dont initialize $b_id if it already comes into the http request.
if (!isset($_POST['b_id']))
{
$b_id = 0;
}
else
{
$b_id = $_POST['b_id'];
}
This way you can alway remember the last selected value of b_id.
Hope this can help you.
I have a php variable "echo $id". Now I want to use the $_POST method to post the variable. I just want to know how to do this for a variable because $_POST[$id] does not work?
I think you are misunderstanding a basic concept here.
The $_POST super global is used to receive input (in the form of a POST request) from the user. While it is possible to set variables in it, you shouldn't.
Your question does not make sense. If you have an HTML form:
<form action="" method="post">
<input type="text" name="something" />
<input type="submit" value="Submit" />
</form>
Then you get the variable $_POST['something'] with whatever the user typed in the text box.
On its own, $_POST is just a variable like any other. You can assign to it $_POST['test'] = 123;, you can delete from it unset($_POST['test']);, you can even make it something other than an array $_POST = "Hello, world";, it just happens to be pre-populated with form data, if any.
With the method $_POST you must be posting to something.
My suggestion to you is to create a form, then have the form going to the file you wish to post to:
So something like this:
echo '<form action = "fileToPostTo.php" method = "post">
<input type = "text" hidden value = "'.$id.'" />
</form>';
And then submit the form when the document loads through jquery or javascript.
You can do it by $_POST['id'] = $id (then You will have it in $_POST['id'] variable (but You shouldn't do it :P).
Or You can send $id by form. Like example:
<form action="/pageToPOST.php" method="post">
<input type="text" value="<?=$id ?>" name="id" />
<input type="submit" name="" value="submit it!" />
</form>
And You'll have $_POST['id'] on http://yourdomainname.com/pageToPOST.php page
you can get and pass the value without page load and form.
<input type="text" name="something" id="something" />
<input type="button" value="ok" onclick="value();"/>
function value()
{
var something=$("#something").val();
var dataparam="oper=show&something="+something;
$.ajax({
type:"post",
url:"yourphpname.pnp",//this is very important.
data:dataparam,
success:function(data)
{
alert(data);
}
});
}
$oper =(isset( $_REQUEST['oper'])) ? $_REQUEST['oper'] : '';
if($oper == "show" and $oper != '')
{
$something=$_REQUEST['something']
echo $something;
}
what you want to do is assign a value submitted to your script using the POST method, to your $id variable. Something like:
$id = $_POST['id'];