Using PHP to loop through values in a POST request - php

I have a form that allows people to enter a keyword.
I'm wanting people to be able to add one keyword at a time and for all keywords they've previously added to be hidden inputs on the form.
Here's my current code:
<form method="post" class="domain-form">
<div class="domain-wrapper">
<input type="text" name="keyword" id="keyword" class="domain-input" autocapitalize="none" autocorrect="off" spellcheck="false" aria-required="true" aria-invalid="false" placeholder="Type a keyword here...">
</div>
<button type="submit" class="button-link button-black getstarted" style="border-top-left-radius:0;border-bottom-left-radius:0;"><span>Add Keyword</span></button>
<input type="hidden" id="domain" name="domain" value="<?php echo $domain; ?>">
<?php
if (isset($_POST['keyword'])) {
foreach($_POST['keyword'] as $inputkeyword) {
echo "<input type=\"hidden\" id=\"keyword\" name=\"keyword\" value=\"$inputkeyword\">";
}
}
?>
</form>
I'm getting an error stating Invalid argument supplied for foreach().
What am I doing wrong?

You need to modify your name structure. The name must have [] on it to create an array.
name="keyword[]"
otherwise it is a plain input and the duplicates overwrite.
This can be found in the documentation here, https://www.php.net/manual/en/faq.html.php#faq.html.arrays.

First of all you should enter action attribute in form, name attribute of button & add name="keyword[]" in your input type text also like below then try
<form method="post" action="" class="domain-form">
<input type="text" name="keyword[]" id="keyword" class="domain-input" autocapitalize="none" autocorrect="off" spellcheck="false" aria-required="true" aria-invalid="false" placeholder="Type a keyword here...">
<button type="submit" name="keyword" class="button-link button-black getstarted" style="border-top-left-radius:0;border-bottom-left-radius:0;"><span>Add Keyword</span></button>

You can use to javascript to achieve this. just add an "add keyword" button for that input and put a click listener on it when the button is clicked you create and append the hidden input to the DOM

Related

php search form using post variable to make an sql query

this is a page that displays a list of creatives, and the form offers search functionality to search by job title:
if(isset($_POST['creatives-submit'])){
$job = $_POST['job-title'];
$data = \Db::Common($fms5->DBH)->getWhere("creatives", "creatives_active", "Yes"," AND creatives_job LIKE '%".$job."%'")->orderBy('creatives_name', 'asc');
}
<form method="post" name="creative-search">
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
is there anything that's obviously wrong my my code?
try changing if(isset($_POST['creatives-submit'])) to if(isset($_POST['job-title']) && !empty($_POST["job-title"])) as the form is posting the job-title value and this is the value you actually care about. (Since creatives-submit will always = Submit)
also change
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
to <input class="form-control" type="text" name="job-title" id="job-title" placeholder="Search by job title" required/>
this means the form can't be submitted unless the job-title field has a value and had the correct type of text
Below is a modification of your code that just returns what the user searched for (Since I don't have it connected to a database)
<?php
if(isset($_POST['job-title']) && !empty($_POST["job-title"])){
$job = $_POST['job-title'];
?>
<p>You Searched For <?php echo $job;?></p>
<?php
}
?>
And the form
<!-- Search Form -->
<form method="post" name="creative-search">
<input class="form-control" required="required" type="text" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</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

How to make placeholder for search input in WordPress

I've override default wordpress search form by creating my own search form (found this code in google). here it is:
<form method="get" class="searchform" action="<?php bloginfo('url'); ?>">
<div>
<input class="text" type="text" value="<?php if(trim(wp_specialchars($s,1))!='') echo trim(wp_specialchars($s,1));else echo ' ';?>" name="s" id="s" />
</div>
</form>
I tried to add a place holder for this & change my code for input line to this:
<input class="text" type="text" value="<?php if(trim(wp_specialchars($s,1))!='') echo trim(wp_specialchars($s,1));else echo ' ';?>" name="s" id="s" placeholder="Search Here" />
but it is not working for me. please help someone.
you can simply use the placeholder attribute like so:
<input type="text" placeholder="Search the site"/>
If you put a value, the value will overwrite your placeholder. Try to delete the value from your input and it should work
You can just use placeholder on your input tag like
<input type="text" placeholder="Search the site"/>

Submit button, next step

I am done with the search page where the user enters the information and select from the drop-list. I've also added the button AddList where you can have more than one search form with tag names changed. All of the searches will eventually be executed in one Submit button and each search will go in one single query. My table caries all the information and tuples contain only numbers.
UPDATED: I tried changing the input type of the input tags but the enable and disable functions can't seem to work on integers, only on text fields. How can I fix that?
My submission is tomorrow, and here is my search code:
<script type="text/javascript">
$('#exactButton').live('click', function(){
$(this).prev().prev().prev().prev().prev().removeAttr('disabled');
$(this).prev().prev().prev().attr('disabled',true);
$(this).prev().prev().prev().prev().attr('disabled',true);
});
$('#rangeButton').live('click',function(){
$(this).prev().prev().removeAttr('disabled');
$(this).prev().prev().prev().removeAttr('disabled');
$(this).prev().prev().prev().prev().attr('disabled',true);
});
})
</script>
And this is my HTML code:
<button id="button">Add List</button><br><br>
<form id ="form" name="search" method="get" action="test.php">
<div id="div">
<select name ="select" >
...options...
</select>
Value:<input type="text" name="exact" id="exactField" />
From: <input type="text" name="from" id="fromField" />
To: <input type="text" name="to" id="toField" />
<br>
<input type="button" name="answer" value="Range" id="rangeButton" />
<input type="button" name="answer" value="Exact" id="exactButton" />
</div>
<br>
<input type="submit"name="search" value="Submit">
</form>
Thank you in advance..
as Dagon said, you will see all submitted parameters in the URL since you are submitting the form with method GET. here is very good explanation for this: http://www.w3schools.com/php/php_get.asp
One idea:
add some custom attributtes to the elements (to the clones too).
this.attr("mycustomattribute","somevalue");
after this, you can get all elements on the page with your custom attribute and your value.
divs = $('div[mycustomattribute="somevalue"]'); //should give all div container with attribute "mycustomattribute" with value "somevalue"
divs.each(function(){
console.log(this,$(this).attr('name'));//show expression (for debug)
});
then you can collect this elements, serialize it and add it to your post Not tested, but an idea.
Kind Regards
In PHP, it's already there.
print_r($_GET); will list all parameters sent by GET method
print_r($_POST); will list all parameters send by POST method.
Then, of course, you will need to iterate in the array to include each values in your query statement.
You can naming input with prefix or suffix correspond to sequence that user click to add list and add those set of inputs to only form.
<form id ="form" name="search" method="get" action="test.php">
<div>
<select name ="select[1]" >
...options...
</select>
Value:<input type="text" name="exact[1]" class="exactField" />
From: <input type="text" name="from[1]" class="fromField" />
To: <input type="text" name="to[1]" class="toField" />
<br>
<input type="button" name="answer[1]" value="Range" class="rangeButton" />
<input type="button" name="answer[1]" value="Exact" class="exactButton" />
</div>
<div>
<select name ="select[2]" >
...options...
</select>
Value:<input type="text" name="exact[2]" class="exactField" />
From: <input type="text" name="from[2]" class="fromField" />
To: <input type="text" name="to[2]" class="toField" />
<br>
<input type="button" name="answer[2]" value="Range" class="rangeButton" />
<input type="button" name="answer[2]" value="Exact" class="exactButton" />
</div>
.
.
.
<br>
<input type="submit"name="search" value="Submit">
</form>

form data not getting passed

I'm practicing form validation with JavaScript but when I try to retrieve the data from the page it submits to I can't get it.
form.html
<body>
Hello.<br />
<form onsubmit="return validate()" action="process.php" method="POST">
Enter name: <input type="text" id="name" /><br />
Enter phone number: <input type="text" id="number" /><br />
Enter password: <input type="password" id="paswd" /><br />
Is there anything else you would like to add: <input type="text" id="anything" /><br />
<input type="submit" value="Check Form" />
</form>
</body>
process.php
<?php
echo 'Here: '.$_POST['number']
?>
Whatever index I use I get " Undefined index: line 2". What am I doing wrong?
EDIT: So I can't use the id attribute I need the name? Is there anyway to prevent coding redundancy since the value of all names will be the same as the corresponding id?
You need name attribute in your fields
<input type="text" id="number" name="number" />
$_POST looks for the name attribute in the field to capture the field values and not id
Your inputs need the name of the element.
Such as:
<input type="text" id="number" name="number" />
The Php gets the form data looking these names, not the ids.
you forgot name of input:
<input type="text" id="number" name="number" />
You need to give your form elements names.
<input type="password" id="paswd" name="paswd" />
Interestingly names and ids share the same namespace. If you don't really need the ids, leave them be. Inside a validate function you can always access all elements with the elements object of the form.
// called onsubmit
var validate = function(e) {
if (this.elements["paswd"].value.length < 4) {
alert("password needs to have at least 4 characters");
return false;
}
return true
};
I usually append the input type to my ids to differentiate them from field names
<label for="paswd-txt">Password: </label>
<input type="text" name="paswd" id="paswd-txt" />
<label for="save-cb">Remember me: </label>
<input type="checkbox" name="save" id="save-cb" value="1"/>
So like Vitor Braga said your inputs need the name of the element, but you only need this if you are using PHP to hadle the values of form in the submit, if you are using javascript to validaate like you said your were praticing you can obtain the value like this:
document.getElementById("number").value

Categories