Writing a dropdown form to translate current page; Google thinks I'm translating from English to English - html

I'm trying to write a dropdown form with a submit button that uses Google translation to translate the current page that I'm on. Here's what I currently have (someone helped me with this):
<form action="http://www.google.com/translate_c" method="get">
<input type="hidden" name="hl" value="en" />
<input type="hidden" name="u" value="<?php echo curPageURL(); ?>" />
<select name="langpair">
<option value="en%7Cafr">English to Afrikaans</option>
<option value="en%7Calb">English to Albanian</option>
...
</select>
<input type="submit" value="Submit" />
</form>
(Echo calls the URL of the current page:)
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
Why does Google think I'm trying to translate from English to English with this code?

I modified your code to the following and it works fine:
<form action="http://www.google.com/translate_c" method="get">
<input type="hidden" name="hl" value="en" />
<input type="hidden" name="sl" value="en" />
<input type="hidden" name="u" value="http://www.stackoverflow.com/" />
<select name="tl">
<option value="af">English to Afrikaans</option>
<option value="sq">English to Albanian</option>
</select>
<input type="submit" value="Submit" />
</form>
I would recheck the curPageURL function and put in the right values for each of the select box items.

Related

html 5 datalist value

how can I get the "id" instead of "name" when i submit the form.
<input type="text" list='name' name='test1'/>
<datalist id="test1">
<?php
if(!empty($data)){
foreach($data as $flag){
echo "<option label ='".$flag['id']."' >".$flag['name']."</option>";
}
}else{
echo "<option> empty </option>";
}
?>
</datalist>
I have tried using value = $flag['id'], but I still get "name" when i submit it.
The id is not sent to the server with a regular submitting of a form. Only the name- and value attributes are.
If you really wanted to get the ids too, you could have a hidden <input> element with an array for value, containing all the relevant ids you want.
Here's an example:
HTML
<form method="post">
<input type="text" name="firstname" id="inp1" />
<input type="text" name="surname" id="inp2" />
<input type="hidden" name="ids[]" value="inp1" />
<input type="hidden" name="ids[]" value="inp2" />
<input type="submit" />
</form>
PHP
foreach($_POST['ids'] as $id)
{
echo '<option label="' . $id . '">' . array_search($id, $_POST) . '</option>';
}

How to change multiple form elements via single select

I'm completely stumped at how I can change the opening tag to have 'method=post' if the second select option is chosen. I am using an Onchange for the form action, but can't figure out how to change the post method too.
My latest attempt is with an IF statement as shown in the below code, but again, only 1 option will work.
if(this.value){
$content .='<form action="/gsresults">';
}
else{
$content .='<form action="/imagesearch?go" method="post">';
}
$content .='<input type="text" name="q"/>
<input type="hidden" name="cx" value="partner-pub-xxxxxxxxxxx" />
<input type="hidden" name="cof" value="FORID:11" />
<input type="hidden" name="ie" value="UTF-8" />
<select onchange="this.form.action=this.value">
<option value="/gsresults" selected="selected">Google</option>
<option value="/imagesearch?go">Images</option>
</select>
<input type="submit" name="sa"/>
</form>';
Thanks for any help/pointers.
Chris
You can use the jQuery to listen for an event and edit the form's attributes:
$("select").change(function() {
$("form").attr("method","post");
});
I'd suggest adding in an id on both your <form> and your <select> so your listeners don't get confused.

How to GET data but preserve link array

Let's say I have index.php?couple=old and I want to GET new data (search all couples that are "old" but get new data) for example
<form action="index.php?couple=old" method="get">
<input type="text" name="search" >
<input type="submit" name="search_btn" />
</form>
And someone enters keyword "Smith" it should be index.php?couple=old&search=Smith
couple must be a variable to pass, otherwise PHP does not store it as part of $_GET.
You need:
<form action="index.php" method="get">
<input type="hidden" name="couple" value="old" />
<input type="text" name="search" />
<input type="submit" name="search_btn" />
</form>
maybe little bit more better solution (if you have couple of parameters you want to use).
<form action="index.php" method="get">
<?php foreach($_GET as $name=>$value):?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>" />
<?php endforeach;?>
<input type="text" name="search" />
<input type="submit" name="search_btn" />
</form>

mysql 404 error

have the following code:
<?php
if (isset($_POST['submitted'])) {
$reqwidth = $_POST['reqwidth'];
$reqside = $_POST['reqside'];
$reqrad = $_POST['reqrad'];
$sqlinsert = "INSERT INTO tirelist (width, sidewall, radial) VALUES ('$reqwidth','$reqside', '$reqrad')";
if (!mysqli_query($dbcon, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "1 record added to database";
}
?>
<html>
<head>
<title>Request Tire Size</title>
</head>
<body>
<h1>Request Tire Size</h1>
<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>Request Tire</legend>
<label>Tire Width: <input type="text" name="reqwidth" /></label>
<label>Tire Sidewall: <input type="text" name="reqside" /></label>
<label>Tire Radial: <input type="text" name="reqrad" /></label>
</fieldset>
<br />
<input type="submit" value="Send Order Request" />
</form>
when i click send order request, I get a 404 error.
i noticed the page is called ~http://localhost/index.php?p=test2~, but when i click it redirects me to ~http://localhost/insert-data.php~
been trying for hours, wondering what i could do to fix it
Change
<form method="post" action="insert-data.php">
to have index.php?p=test2 instead.
In your form, you reference insert-data.php. Does that exist?
If you're trying to submit the form to the same file, you can try to use this:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Try this instead. form method="post" action="/insert-data.php"

mySQL query returns only one result

I am trying to get each row in a table to appear as part of a survey. The following code is returning only the first row in the table (so users can see only one question). I've been over and over this and can't see what I'm doing wrong. Would much appreciate any input.
Thank you!
function getQuestions ($dbc) <!--$dbc=database connection--> {
$query = "SELECT * FROM survey_questions" <!--survey_questions=table--> ;
$result = #mysqli_query ($dbc, $query);
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC) ) {
$body = $row ['question_body'] <!--question_body=row in table--> ;
echo '
<div class="entry"> <!--user entry form-->
<h3 class="qTitle">'.$body.'</h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
';
}
}
First of all you don't need to echo so much using php...
You have to use // or /* */ to comment in PHP and not <!----> cuz that's for HTML
Secondly coming to your code..
Why you are using?
function getQuestions($dbc) //I dont know what this is doing here, why you are wrapping your code in a function???
you can simply write like this (use echo to print out your question):
<?php
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
?>
<div class="entry"> <!--user entry form-->
<h3 class="qTitle"><?php echo $row['whatever']; ?></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
<?php
}
?>
The results are now two entry forms (an improvement) but still not displaying the text of the two rows from the database. Instead of the row text, I get the > character where the text should be. Here is the updated code, adapting the suggestion of #Mr. Alien:
function getQuestions($dbc) {
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
echo '
<div class="entry">
<h3 class="qTitle">'. $row['survey_questions'].'></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" >
<input type="submit" value="Submit" name="submit" >
<input type="hidden" name="questionid" value="questionid" >
<input type="hidden" name="submitted" value="1" >
</form>
</div>
';
}
}