Updating the correct fields in database - php

I have few fields in a form say
<form action="?code='.$order_id.'" method="post">
<input type="text" name="length" placeholder="'.$row_order['length'].'">
<input type="text" name="breadth" placeholder="'.$row_order['breadth'].'">
<input type="text" name="width" placeholder="'.$row_order['width'].'">
<input type="submit" name="save" value="Save">
</form>
I retrieve the value of length, breadth and width from database and show it through a placeholder. Now I want if the user enters some value say in length then only length field in DB to be affected others being intact.
Now if I put the value of length as 50 and "Save" it then it shows length=50, breadth=0 and width=0 even if I had breadth=40 and width=30 before updating.

Instead of placeholder use value in input tag
<form action="?code='.$order_id.'" method="post">
<input type="text" name="length" value="'.$row_order['length'].'">
<input type="text" name="breadth" value="'.$row_order['breadth'].'">
<input type="text" name="width" value="'.$row_order['width'].'">
<input type="submit" name="save" value="Save"> </form>

Related

variables containing Post elements get empty after another post called

i have a problem keeping a post element in variables before calling one more time post in a second form .
briefly:
variable below contain elements from previous post
$img=isset($_POST['image'])?$_POST['image']:false;
$table=isset($_POST['tabl'])?$_POST['tabl']:false;
Then i have as next to the lines above the form below :
<form method="post" action="" style="padding-top:200px;padding-bottom:200px;padding-left:500px">
<div><span>Email</span></div> <input type="text" value="" name="user"/><br><br>
<div><span>Password</span></div> <input type="password" value="" name="pass"/><br><br>
<input type="submit" value="send" name="submit"/>
</form>
and right after pressing submit $img and $table are both empty .
how can i keep the values in $imgand $table even after calling post again ?
any clue ?
Thanks
You aren't sending the variables you want in the POST data in your second form.
You could use a hidden field to send it,
A bit like this.
<form method="post" action="" style="padding-top:200px;padding-bottom:200px;padding-left:500px">
<input type="hidden" name="img" value="<?php echo $img;?>" />
<input type="hidden" name="table" value="<?php echo $table;?>" />
<div><span>Email</span></div> <input type="text" value="" name="user"/><br><br>
<div><span>Password</span></div> <input type="password" value="" name="pass"/><br><br>
<input type="submit" value="send" name="submit"/>
</form>

how to append search to end of current url

How would I append the search string to the end of the url instead of deleting the current posts
<input class="searchBox" type="text" name="search" placeholder="Search Here">
<input type="submit" value="">
I have this at the moment and it deletes the other Post variables at the end of the url, but what I want it to do is append it to the end
So for example, I have page.php?id=123 .. the user searches and I want it to display page.php?id=123&search=text, but at the moment it replaces it and does page.php?search=text
you can insert a hidden input before the other inputs. Also your form must submit using get to achieve what you are looking for.
<form method="get" action="">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input class="searchBox" type="text" name="search" placeholder="Search Here">
<input type="submit" value="Search">
</form>
Add
<input type='hidden' name='id' value='".$_GET['id']."'>;
Into the form

PHP Form URL vars

I was wondering if it would be possible to have a variable in the URL created when submitting a form?
form:
<form class="register_form" action="action.php" method="get">
Team Name*: <input type="text" name="teamname" required />
Team Region*: <input type="text" name="teamregion" maxlength="4" required />
Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
Team Members: <input type="text" name="teammembers" />
<input name="register_submit" type="submit" value="Register" />
</form>
I'd like the link to end up as: http://.../action.php?do=register
My reasoning for this is so that I can use action.php for more than one thing using if statements. Thanks ^^
Just append the variable you want to the action link.
<form class="register_form" action="action.php?do=register" method="get">
Team Name*: <input type="text" name="teamname" required />
Team Region*: <input type="text" name="teamregion" maxlength="4" required />
Team Leader*: <input type="text" name="teamleader" maxlength="16" required />
Team Members: <input type="text" name="teammembers" />
<input name="register_submit" type="submit" value="Register" />
</form>
Or you can add a hidden field to your form:
<input type="hidden" name="do" value="register" />
Sure, the form action URL can have a query string:
<form class="register_form" action="action.php?do=register" method="POST">
The form data will be sent via POST but do will still be available via GET.
You need to add this to the form
<input type="hidden" name="do" value="register">
Yes, it is possible. You can use any one of following methods
1) You can set the name of your submit button "do"; As the value of your submit button is "Register"
<input type="submit" name="do" value="Register" />
OR
2) You can add a hidden field to your form
<input type="hidden" name="do" value="register" />

Multiple search inputs to output as one search in wordpress

I'm trying to create a search box that would have 4 inputs but output as one search. Here is my current code:
<form method="get" id="searchform" action="http://sitename.com/">
<input type="text" name="s" />
<input type="text" name="a" />
<input type="text" name="b" />
<input type="text" name="c" />
<div class="clear_space"></div>
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />
</form>
But that outputs like this in wordpress: /?s=milk&a=eggs&b=cheese&c=garlic&submit=Search
But I need it to output like this: /?s=milk+egg+cheese+garlic&submit=Search
So somehow those additional inputs need to add their text to the first ?s= with just a +... Any help is massively appreciated.
Are you kidding??
The + that you are seeking is simply a space. Have one text field, and when the form is submitted the spaces in between the words will be converted into +.
Wow, you have a lot of problems, but before we get deep into them let me tell you that your "output" is in fact a get parameter, or a URL. Now you should NEVER use the same id for different tags (!!!). If you think about a valid reason to do that, then think again.
Change your form as follows:
<form method="get" id="searchform" action="http://sitename.com/">
<input type="text" class="field" placeholder="Search" />
<input type="text" class="field" placeholder="Search" />
<input type="text" class="field" placeholder="Search" />
<input type="text" class="field" placeholder="Search" />
<input type="hidden" name="s" id="s" />
<div class="clear_space"></div>
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />
</form>
and then implement a form submit event to calculate your value and assign to your input field. Cheers.
EDIT:
Use the following js code to run before you submit your form:
//...
var value = "";
$(".field").each(function(){
value += ((value !== "") ? (" ") : ("")) + $(this).val();
});
$("#s").val(value);
//...

Multiple $_POST on same page PHP

I have a question.
Currently this form will be dynamically generated.
Example,
<form method="POST">
<input type="text" name="location" id="location1" />
<input type="submit" value="Submit!" />
<input type="text" name="location" id="location2" />
<input type="submit" value="Submit!" />
<input type="text" name="location" id="location3" />
<input type="submit" value="Submit!" />
<input type="text" name="location" id="location4" />
<input type="submit" value="Submit!" />
</form>
So whenever i press submit, it will take last value of form only. How do i make it take all $_POST?
Thank you.
You could give each field a unique name: location1, location2....
Alternatively, you could build an array.
To do that, add a [] to each element's name:
<input type="text" name="location[]" id="location1" />
this will give you an array in $_POST["location"].
Don't forget that before using the data (e.g. in a database query or page output) you will need to sanitize each array element separately (using mysql_real_escape_string() or htmlspecialchars() or whatever is needed in your situation.)
Give each input its own name or use:
<input type="text" name="location[]" id="location1" />
Then PHP will treat it like an array.
Either give each input a different name attribute, for example location1, location2, location3 and location4 then access with $_POST['location1'] $_POST['location2'] etc.
Alternatively (and probably preferred if this form is being generated 'dynamically'), change the name attribute for each input to location[] then access the values entered as an array in PHP. For example...
HTML
<form method="post">
<input type="text" name="location[]" id="location1" />
<input type="submit" value="Submit!" />
<input type="text" name="location[]" id="location2" />
<input type="submit" value="Submit!" />
<input type="text" name="location[]" id="location3" />
<input type="submit" value="Submit!" />
<input type="text" name="location[]" id="location4" />
<input type="submit" value="Submit!" />
</form>
PHP
print_r($_POST['location']);
echo "<form method='post'>";
for($x=1;$x<=4;$x++)
{
echo "<input type='text' name='location".$x."' id='location".$x."'/>";
echo "<input type='submit' value='Submit!' />";
}
echo "</form>";

Categories