I hope someone can help me, I have an live Ajax Jquery search bar. Now my search bar shows no more then 5 results. What I want is an button below the live results with 'More results', and if you click this button you go to a new page and see all te results of your search.
What I have tried is to put an submit button below the row results, but then he put all the names of the results into the search insteady of the text from the input.
PHP:
<form action="./s/" method="GET">
<div class="searchbox">
<!-- SEARCHBOX INPUT -->
<input type="text" name="s" class="searchbox-input" placeholder="Search trough 1,000 games in our database.." name="name" />
<div class="searchbox-line"></div>
<div class="searchbox-icon">
<input class="img-searchbox-icon" name="" value="" type="submit">
</div>
<div class="results"></div>
</div>
</form>
MySQL:
if(isset($_REQUEST["term"])){
// Prepare a select statement
$sql = "SELECT * FROM games WHERE name LIKE ? ORDER BY name LIMIT 5";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_term);
// Set parameters
$param_term = '%' . $_REQUEST["term"] . '%';
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
// Check number of rows in the result set
if(mysqli_num_rows($result) > 0){
// Fetch result rows as an associative array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo '<a href="' . $row["url"] . '">';
echo '<p class="results-name">' . $row["name"] . '</a></p>';
echo '<p class="results-platform">';
echo '<img class="platform" src="img/platform-icons/' . $row["platform1"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform2"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform3"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform4"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform5"] . '.png" />';
echo '<img class="platform" src="img/platform-icons/' . $row["platform6"] . '.png" />';
echo '</p>';
}
} else{
echo '<p class="results-gamename">No matches found</p>';
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
Alright, I'll take a shot at this. In your HTML file (the one you originally named 'PHP') you've declared the form like this:
<form action="./s/" method="GET">
And you've given your search input the following name:
<input type="text" name="s" class="..." placeholder="..." name="name" />
Hence, when you send through the form, your input field will be sent through with the name 'name', since you've double declared its name. It'll fallback to the last 'name' attribute you've given to the element.
Related
I have an issue when trying to insert info coming from a form which is generated dynamically in php.
The form consists of of an variable amount of inputs which all consists of four input elements. See below how my form is generated:
<?php $result = mysql_query("SELECT id,name,description FROM todo_q WHERE todo_id = $todo_id AND active = 'y'");
while($todo_q=mysql_fetch_array($result)){
echo '<label>';
echo $todo_q['name'];
echo '</label><br>';
echo '<input type="checkbox" name="value[]" value="y" />';
//echo '<input type="hidden" name="value[]" value="n" />';
echo '<label>';
echo $todo_q['description'];
echo '</label><br>';
echo '<input type="text" id="comment" name="comment[]">';
echo '<input type="hidden" name="user_id[]" value="';
echo $user_id;
echo '" />';
echo '<input type="hidden" name="todo_id[]" value="';
echo $todo_q['id'];
echo '" />';
echo '<HR>';
}?>
And this is how I try to insert the info into mySQL:
$query = "INSERT INTO todo_a (value, comment, user_id, todo_id) VALUES ";
$query_parts = array();
for($x=0; $x<count($_POST["value"]); $x++){
$query_parts[] = "('" . $_POST['value'][$x] . "','" . $_POST['comment'][$x] . "'," . $_POST['user_id'][$x] . "," . $_POST['todo_id'][$x] . ")";
}
$q_parts = $query_parts;
foreach ($q_parts as $q_p){
$insert = ($query .= implode(',', $query_parts));
$result = mysql_query($insert);
}
The problem I have is that when check all checkboxes and comments everything is inserted on the right row in the DB, but if I skip to check one checkbox then it gets messed up...
I would like it the insert a new row if it the checkbox is checked and/or a comment is entered.
Can anybody point me in the right direction?
I tried to put a hidden input to get the value of unchecked checkboxes but i doesn't seem to work.. That why I have commented out the hidden checkbox.
PS. I know I should be using mysqli but this is an older site that I haven't upgraded yet..
You need to add index into input checkbox and comment name like :
$cbIndex = 0;
while($todo_q=mysql_fetch_array($result)){
echo '<label>';
echo $todo_q['name'];
echo '</label><br>';
// Generate checkbox with index of current result
echo '<input type="checkbox" name="value[' . $cbIndex . ']" value="y" />';
// Generate comment with index of current result
echo '<input type="text" id="comment" name="comment[' . $cbIndex . ']">';
echo '<input type="hidden" name="user_id[' . $cbIndex . ']" value="';
echo $user_id;
echo '" />';
echo '<input type="hidden" name="todo_id[' . $cbIndex . ']" value="';
echo $todo_q['id'];
echo '" />';
echo '<HR>';
// Inc of index
$cbIndex++;
}
When you submit your form, only checked checkbox will appear in $_POST["value"] :
foreach ($_POST["value"] as $cbIndex => $cbValue) {
$query_parts[] = "('" . $_POST['value'][$cbIndex] . "','" . $_POST['comment'][$cbIndex] . "'," . $_POST['user_id'][$cbIndex] . "," . $_POST['todo_id'][$cbIndex] . ")";
// or
$query_parts[] = "('" . $cbValue . "','" . $_POST['comment'][$cbIndex] . "'," . $_POST['user_id'][$cbIndex] . "," . $_POST['todo_id'][$cbIndex] . ")";
}
...
Btw, you don't need to store value of checkbox, that will be 'y' all the time.
INFO That will be fine for a test app, but as commented by #Pogrindis and #John Conde, it's not safe code. MySQLi/PDO + prepare statement will avoid SQL injection.
I have an ideal search-box which searches item from a given listing. My searching is working fine, the problem is when I enter something in search box to search, it gives me result but my search box gets empty.
For eg If I search "Electronics" in search box, it gives me result of electronics but my search box gets empty. It should be written with "Electronic" when it gives me result.
Probably, I should be using GET method instead is it so?
Here is my code for searching:
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
if (!empty($_REQUEST['term']))
{
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM category WHERE cat_name LIKE '%" . $term . "%' or parent LIKE '%" . $term . "' or cat_status LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1)
{
$dynamicList="";
while ($row = mysql_fetch_array($r_query))
{
// $cat_id=;
/*$dynamicList .= '
<img style="border:#666 1px solid;" src="../storeadmin/category/thumbs/' . $row['cat_id'] . '.jpg" width="77" />';*/
echo "<tr bgcolor=''>";
echo "<td>" . $row['cat_id'] . "</td>";
echo "<td><img style='border:#666 1px solid;' width='70' src='http://localhost/jaymin/My%20Store/storeadmin/category/thumbs/". $row['cat_id'].".jpg' /></td>";
//echo "<td>".$dynamicList."</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['parent'] . "</td>";
echo "<td>" . $row['cat_status'] . "</td>";
echo "<td><a href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
}
else {
echo "Nothing should be displayed";
}
?>
</table>
change your code to
Search: <input type="text" name="term" value="<?php echo #$_REQUEST['term']; ?>" /><br />
instead of
Search: <input type="text" name="term" /><br />
Try this,
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>
If you want this in your url use GET instead of POST like,
<form action="" method="get">
Search: <input type="text" name="term" value="<?php if(isset($_GET['term'])){ echo $_GET['term']; } ?>" /><br />
<input type="submit" value="Submit" />
</form>
This is because you are submitting the form and after form submit textbox values disappears. To overcome this try:
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>
I am having a table with <input type="text" name="' . $r['0'] . '" value="' . $r['0'] . '"
populated from data that i fetch from database like this:
echo '<form id="actions" name="nonValidMainForm" method="post"><table border="2" width="100%">';
echo "<tr><td><b>Index</b></td><td><b>Email </b></td> <td><b>Date</b></td> <td><b>Name</b></td> <td><b>Surname</b></td> <td><b>Telephone Number</b></td> <td><b>Street Adress</b></td><br/>";
while($r = mysql_fetch_array($result)) {
$my[] = $r['0'];
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="' . $r['0'] . '" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo "<pre>";
print_r($my);
echo "</pre>";
if(isset($_POST['unsubscribe'])){
foreach($my as $key=>$value){
$email = $value;
}
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
echo '<button style="position:fixed;bottom:5;left:5;">Change</button>';
echo '</table></form>';
The table looks like this:
I have tried this:
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
But the value is empty
So each time i press unsubscribe button the corresponding email to be deleted. How is this possible?
Your form has many elements with the same name. How can the browser determine which element's value to send to the server when the form is posted? Generally the last one takes precedence, but I suspect that behavior may be undefined and browser-specific.
If each individual table row needs to be a separately post-able form, then each row needs its own form:
echo '<td>
<form method="POST" action="somePage.php">
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</form>
</td>';
That way when the browser posts the form to the server, it knows specifically which email and unsubscribe elements to use. Since there's only one of each for that form.
You have to wrap your inputs in a <form> tag.
echo '<form>';
while($r = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$roww++.'</td>';
echo '<td>
<input size="50%" type="text" name="email" value="'.$r['0'].'">
<input type="submit" name="unsubscribe" value="Unsubscribe">
</td>';
echo '<td>'.$r['1'].'</td>';
echo '<td>'.$r['2'].'</td>';
echo '<td>'.$r['3'].'</td>';
echo '<td>'.$r['4'].'</td>';
echo '<td>'.$r['5'].'</td>';
echo '</tr>';
}
echo '</form>';
if(isset($_POST['unsubscribe'])){
$email = $POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' );</script>";
}
Based on your code above it looks like it's a syntax error. Try the update below
if(isset($_POST['unsubscribe'])){
$email = $_POST['email'];
echo "<script>console.log( 'Value is: " . $email . "' ); </script>";
}
I'm making a comment box, in which
Comment box will be under the post
Every comment box has the same id as the post has
That's why i preffered to do a while loop
But, the problem is
As i'm using a fetch_assoc() method, some comment tables will be empty so
i've made a if else code for them
Here is my if else code :
$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . "ORDER BY id";
$result_query_select_comment_table = $dbc->query($select_comment_table);
if(!$result_query_select_comment_table) {
$result_select_comment_table = array("full_name" => "", "comment"=> "No comments yet.");
}
else {
$result_select_comment_table = $result_query_select_comment_table->fetch_assoc();
}
If other code is need :
echo '<div id="feed_comment_box_' . $row['id'] . '"' . 'class="feed_comment_box_cl"><div id="add_comment_id" class="add_comment_cl">
<form class="comment_form" method="post" action="' .$_SERVER['PHP_SELF'] . '">
<input name="comment_full_name" type="text" class="input_comment_full_name"> </input>
<textarea name="input_comment_text" type="text" class="input_comment_text" ></textarea><input name="comment_submit" type="submit"></input> <br>
</form>
</div><br>
<div id="comment_box_id" class="comment_box_cl">
<table tabindex="0" class="comment_box">
<tr> <td class="comment_full_name">' . $result_select_comment_table["full_name"] . '</td></tr><br>' .
'<tr> <td class="comment_full_name">' . $result_select_comment_table["comment"] . '</td></tr><br>'
. '</table></div></div>';
echo '</div>';
Problem : As you see the if else code, the php should echo no comments yet when there is no comment. But, even after inserting the comment(i tried manually insert in database) it is showing no comments yet whereas it should show the comments
Note: I am using a while loop that's why the comment div is coming under every post.
It looks like you're missing a space in your query:
$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . "ORDER BY id";
should be
$select_comment_table = "SELECT * FROM feed_comment_" . $row['id'] . " ORDER BY id";
The invalid query is causing $result_query_select_comment_table to always be false.
Here is the code from my submit.php file that has the form mentioned in the title. The if and while statements work and all of this code works the way it looks.
<form action="file.php" method="post">
<?php //database connection stuff
...
if($result->num_rows > 0){
echo '<div class="pure-g">';
while($row = $result->fetch_assoc()){
echo '<div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-5">
<fieldset id="' . $row[Name] . '">
<h3 class="content-subhead">'
. $row[Name] . '
</h3>
<img src="' . $row[Picture] . '" width="100" height="100">
<input type="checkbox" name="check" id="check' . $row[Name] . '" value="' . $row[Name] . ',">
</fieldset>
</div>';
}
echo '</div>';
}
else{
echo "0 results";
}
?>
</div>
<input type="submit" value="Add Selected Games">
</form>
The problem I'm getting is when I use the submit.php:
$conn = mysql_connect($host, $name, $pass);
if(!$conn){
echo "cannot connect to server" . mysql_error();
}
$select = mysql_select_db($db);
if(!$select){
echo "cannot select db" . mysql_error();
}
$list = $_REQUEST['check'];
echo " success $list";
There are currently 3 checkboxes showing on the file.php page that will list the fieldset objects. How should I name the checkbox to have it echo the right $row[Name]? It will echo one $row[Name] based on the a checkbox being checked and newest checkbox (created later in the while loop).
When you just want to see an unformatted list or an array you must convert the array to text.
$list = var_export($_REQUEST,false);
echo "sucess $list";
The checkbox value should "1"
The checkbox name should include the identifier
<input type="checkbox" name="check' .$row[Name] . '" id="check' .$row[Name] . '" value="1">
When the form is submitted you get the check value like this:
$list = "<ul>\n";
foreach $_POST as $key => $val){
if (substr($key,0,5)=='check'){
$list .= "\n<li>" . substr(($key,5);
}
}
echo "sucess\n$list\n</ul>";
The Browser only posts the check boxes that are checked. Some Browsers do not pass the Value. The value that is passed with the checkbox key name varies from Browser to Browser.
I also cleaned up the form routine:
<?php
header('Content-Type: text/html; charset=utf-8');
header('Connection: Keep-Alive');
header('Keep-Alive: timeout=50, max=100');
header('Cache-Control: max-age=120');
echo <<<EOT
<!DOCTYPE HTML>
<html lang="en"><head><title>Title</title><style type="text/css"></style></head><body><div>
<form action="file.php" method="post">
EOT;
if($result->num_rows > 0){
echo '<div class="pure-g">';
while($row = $result->fetch_assoc()){
$name=$row[`Name`];
$pic = $row[`Picture`];
echo <<<EOT
<div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-5">
<fieldset id="$name">
<h3 class="content-subhead">$name</h3>
<img src="$pic" width="100" height="100">
<input type="checkbox" name="check$name" id="check$name" value="1">
/fieldset>
</div>
EOT;
}
echo '</div>';
}
else{
echo '<p>0 results</p>';
}
echo '</div><input type="submit" value="Add Selected Games"></form></body></html>';
?>