Как переопределить параметр name у input в html
Есть такая задача, одно поле , две кнопки При нажатии на одну кнопку ищет в Google , при нажатии на вторую ищет в Яндекс.
Хочется сделать это все без JS. В данном случае не работает кнопка Яндекс, потому что в поле input , name = text должно быть. А в случае с Гуглом , должно быть равно "q"
Вот мой код:
<head>
<!-- Заголовок -->
<title>Поиск</title>
</head>
<!-- Тело -->
<body align= "center">
<h1>Search</h1>
<form id="search" align= "center" action="https://www.google.ru/search"method ="get">
<input size= "30" autofocus name= "q" required placeholder= "Запрос" type="text"/>
</form>
<p><input type= "submit" value="Поиск в Google"/>
<input type= "submit" formaction= "https://yandex.ru/search/" value="Поиск в Yandex" form="search"/></p>
</body>
Ответы (2 шт):
Автор решения: Andinevsky
→ Ссылка
Без js сложно представить, как такое сделать.
<head>
<style>
body {
background-color: #1f1a1a
}
h1 {
background-color: #00b33c;
}
</style>
<!-- Заголовок -->
<title>Поиск</title>
</head>
<!-- Тело -->
<body align= "center">
<h1>Search</h1>
<input size= "30" autofocus id="query" name= "q" placeholder="Запрос" type="text" required>
<p>
<button type= "button" id="googleSearchBtn" onclick="googleSearch()">Поиск в google</button>
<button type= "button" id="yandexSearchBtn" onclick="yandexSearch()">Поиск в Yandex</button>
</p>
</body>
<script>
function googleSearch(){
var query = document.getElementById("query").value;
window.open("https://google.com/search?q="+query, '_blank');
}
function yandexSearch(){
var query = document.getElementById("query").value;
window.open("https://yandex.ru/search?text="+query, '_blank');
}
</script>
Автор решения: Denis640Kb
→ Ссылка
Почему так сразу категорично "Без js невозможно"?
Вариантов реализации куча.
Вот один из примеров php.
<?php
?>
<head>
<title>Поиск</title>
</head>
<body align= "center">
<h1>Search</h1>
<form id="search" align= "center" action="#" method="post" target="_blank">
<input size= "30" autofocus name= "q" required placeholder= "Запрос" type="text"/>
<p>
<button name="google" type="submit" formmethod="post">Поиск в Google</button>
<button name="yandex" type="submit" formmethod="post">Поиск в Yandex</button>
</p>
</form>
</body>
<?php
if(isset($_POST['q']) and !empty($_POST['q'])){
if(isset($_POST['google'])){
header('Location: https://www.google.ru/search?q='.$_POST['q'],true,301);
} elseif(isset($_POST['yandex'])){
header('Location: https://yandex.ru/search?text='.$_POST['q'],true,301);
}
}