Append input from search box into button link - php

I'll make this short and quick. :)
I'm trying to implement a button on my website, which redirects me to a URL I specify + what the user is looking for.
I have this little piece of code here:
<html>
<div class="search">
<form role="search" method="get" id="search">
<div>
<input type="text" value="" name="tag_search" id="tag_search" />
<input type="button" value="Cerca" onclick="window.location.href='https://mywebsite.com/'" />
</div>
</form>
</div>
</html>
that is almost working.
The only thing is that if the User inputs "Hello" in the Search Box, it will always search for the following URL once you press the "Submit" button: https://mywebsite.com/
How can I append what the User has written into the Search Box, so that the Button will redirect me to: https://mywebsite.com/Hello ?
Thank you all!

Add the value of the input to the link
<html>
<div class="search">
<form role="search" method="get" id="search">
<div>
<input type="text" value="" name="tag_search" id="tag_search" />
<input type="button" value="Cerca"
onclick="window.location.href='https://mywebsite.com/' +
document.getElementById('tag_search').value" />
</div>
</form>
</div>
</html>

Add the following Javascript to help
<script>
function goToSearch() {
window.location.href= 'https://mywebsite.com/' + encodeURI(document.getElementById("tag_search").value)
}
</script>
Then your HTML should look like this
<html>
<div class="search">
<form role="search" method="get" id="search">
<div>
<input type="text" value="" name="tag_search" id="tag_search" />
<input type="button" value="Cerca" onclick="goToSearch()" />
</div>
</form>
</div>
</html>

Related

Input in HTML not connecting to PHP post method

I have an HTML input and button:
<form action="validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<form>
<label></label>
<input id="input" name="InputText" type="text"/>
</form>
</div>
<!-- THE BUTTON ITSELF -->
<input type="button" id="button" name="myButton"><b>Search Archive</b>
</form>
in my validate.php file I have this switch statement:
<?php
switch ($_POST["InputText"])
{
case "someval":
http_header("someaddress.com");
die();
break;
}
?>
the problem is that when I click the button it doesn't do anything. I did this with JS and it worked but it should be noted that I'm really new to web development so if anyone can explain to me what I did wrong and specifically why that would be great. Thanks!
You have a form inside of a form, that won't work. Also, you need to include an <input type="submit" value="submit" /> before you close your form. This is what submits the information from the form to your action="file.php".
A form would typically look like this:
file.html
<form action="validate.php" method="POST">
<input type="text" name="username" placeholder="Enter your username" />
<input type="submit" name="submit" value="Submit" />
</form>
Then you'd do something like this:
validate.php
<?php
echo "Your username is" . $_POST['username'];
The $_POST['username'] is the data gathered from the name="username" input from the HTML. If you write die($_POST); you'll get all the data that is sent through the form.
When you are using type='button' you have to perform the submit by yourself.
So, you can do that using javascript or change to type='submit'.
Example:
<input type="button" id="button" name="myButton"><b>Search Archive</b>
To
<input type="submit" id="button" name="myButton" value="Search Archive" />
you can try this
<form action="/validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<label></label>
<input id="input" name="InputText" type="text"/>
</div>
<!-- THE BUTTON ITSELF -->
<button type="submit" id="button" name="myButton">Search Archive</button>
</form>
in the div id ="code" you used form tag that's why its not work...delete it will work and button type must be submit

On click submit go to an url from field

I have a simple form:
<form class="form-contact-warp form-calc-ship cb-form" action="javascript:redirect();">
<input class="form-control" required="" id="textBox" type="text" name="code" size="15" placeholder="USI-TECH affiliate ID">
<button type="submit" class="btn-main-color btn-block"> Create page</button>
</form>
If I put in input field the text daniel, I want to append a link.
Example:
I put daniel in and I click submit. I want to appear below the link
www.example.com/daniel and the text This is your link.
Thanks
I think the best way to do this is to use jQuery or JavaScript by itself:
<form class="form-contact-warp form-calc-ship cb-form" action="" method="post">
<input class="form-control" required="" id="textBox" type="text" name="code" size="15" placeholder="USI-TECH affiliate ID">
<button type="submit" class="btn-main-color btn-block"> Create page</button>
</form>
<div id="response"></div>
<!-- add jquery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
$('form').on('submit',function(e){
e.preventDefault();
$('#response').html('This is your link:<br />http://www.example.com/'+encodeURI($('#textBox').val()));
});
});
</script>
Here is a jQuery demo found at jsFiddle
Here is the same thing, only it changes when you type.
If you want to use purely PHP, you need to check something has been submitted:
<form class="form-contact-warp form-calc-ship cb-form" action="" method="post">
<input class="form-control" type="text" name="code" size="15" placeholder="USI-TECH affiliate ID">
<input type="submit" class="btn-main-color btn-block" value="Create page" />
</form>
<?php
# Check if there is a post
if(!empty($_POST['code'])) {
# You want to make sure you remove possible html and covert the string to a url.
echo 'This is your link:<br />http://www.example.com/'.urlencode(trim(strip_tags($_POST['code'])));
}

How to get text from TinyMCE and save it to database

I'm using TinyMCE text editor on my website and I have problem with gettingtext from the TinyMCE and then insert to the db. I'm propably blind, but a don't see what wrong. Working with PDO.
Header, activate the editor
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
Forms
<?php include "InsertArticles.php"; ?>
<div id="editor">
<form method="post">
<textarea name="Obsah"></textarea>
</form>
</div>
<div id="inputaddnadpis">
<form method="post">
Nadpis: <input type="text" name="Nadpis">
</form>
</div>
<form method="post">
<input type="submit" name="Article" id="InsertArticles" value="Add article">
<input type="submit" name="Tip" id="InsertTips" value="Add tip">
</form>
Insert
<?php
include_once "db.php";
global $db;
if (!empty($_POST["Article"])) {
$sqlVlozeni = "INSERT INTO WEB_ARTICLE (Nazev, Clanek) VALUES (:nazev, :clanek)";
$sqlProvedeni = $db->prepare($sqlVlozeni);
$stav = $sqlProvedeni->execute(array(":nazev" => $_POST["Nadpis"], ":clanek" => $_POST["Obsah"]));
}
?>
Submitting the third form will not submit values from the first two forms.
In general, only the inputs inside a particular form will be submitted with that form.
Consider using only one <form> element around all of your inputs.
<form method="post">
<div id="editor">
<textarea name="Obsah"></textarea>
</div>
<div id="inputaddnadpis">
Nadpis: <input type="text" name="Nadpis">
</div>
<input type="submit" name="Article" id="InsertArticles" value="Add article">
<input type="submit" name="Tip" id="InsertTips" value="Add tip">
</form>

Input field not submitting on enter press

I have a problem with submitting a input field. I know I am not the first one who ask this question but I looked at the answers and they didn't work.
I have an input field and a submit button inside a div. I have the code working so that you can search on button press but I can't get submitting on enter press working.
html:
<div class="search singers" action="templates/search/search_singer.php" method="POST">
<input name="search" type="text" id="search" placeholder="Search here for singers" onkeyup="autosug();" data-file="search_singers"/><input class="button" type="submit" value="Search" onclick="search();"/>
<div id="output" class="output">
</div>
</div>
Jquery code:
var link = '#search';
//post input
$(function(){
$(link).keydown(function(e){
if (e.keyCode == 13) {
$(link).submit();
//He detect an enter press but stops then
}
});
});
I found the Jquery code on this site
And here a similar question: here
problem sovled I came at the idea that I have a function that makes the ajax request to send the data to php. I now call that function on enter press.
Your form should be like this. You forgot the form tag in your code, and that's why the form won't submit.
<div class="search singers">
<form name='your form' action="templates/search/search_singer.php" method="POST">
<input name="search" type="text" id="search" placeholder="Search here for singers" onkeyup="autosug();" data-file="search_singers"/>
<input class="button" type="submit" value="Search"/>
</form>
<div id="output" class="output">
</div>
</div>
And second thing your autonsan function like this
<script>
function autosug() {
alert("You you are here");
}
</script>
You need to use a form. So when you will press enter, then form will be submitted including input field value.
<div class="search singers">
<form action="templates/search/search_singer.php" method="POST">
<input name="search" type="text" id="search" placeholder="Search here for singers" onkeyup="autosug();" data-file="search_singers"/>
<input class="button" type="submit" value="Search" onclick="search();"/>
</form>
<div id="output" class="output">
</div>
</div>

Performs form action even out of the form

Hi having a problem with this code even if i clicked on cancel it will still proceed to use the action of my form even though the CANCEL button is not part of the form, Would appreciate any help.
Here is the code.
<form method="post" action="input_enroll.php" >
<div id="overlay1">
<div>
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" >
<input type="submit" value="Proceed To Enroll" '>
</form>
<input type="submit" value="Cancel" onclick='overlay()'>
</div>
</div>
EDIT: Any suggestions what would be a better idea? I'm thinking putting the cancel outside the div.
You are having form started, then divs started, then form closed..start form after divs..
as your markup is not correct, browsers will change it as thier parser suggest,
In chrome </form> tag is postponed after </div>s..
<div id="overlay1">
<div>
<form method="post" action="input_enroll.php" >
<h1> Enter Educational Level Entry</h1>
<input type="text" name="level" />
<input type="submit" value="Proceed To Enroll" />
</form>
<input type="submit" value="Cancel" onclick='overlay()' />
</div>
</div>

Categories