getting HTML to send content to PHP - php

Okay so I'm confused and lost and have no idea of what I am doing, basically here is my code:
HTML/JavaScript:
<img src="Images/Question1.png" usemap="#mainMap" id="main"/>
<map name="mainMap" id="mainMap">
<area shape="rect" coords="82,192,196,242" onclick="incorrectAnswer()" />
</map
function incorrectAnswer() {
document.getElementById("incorrectVid").style.display="block";
document.getElementById("incorrectVid").play();
document.getElementById("answer_id").value=id;
document.forms["answer_send"].submit();
}
<form id="answer_send" name="answer_send" action="answersend.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/>
<input type="submit" value="Submit" />
</form>
PHP:
$id=$_POST['answer_id'];
echo $id
I have no idea why this is not working any ideas/help?

To grab values submitted via GET/POST you need to assign the name property to your input fields. Try the below:
The HTML and JS
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
function incorrectAnswer() {
document.forms["answer_send"].submit();
}
</script>
</head>
<body>
<img src="none.jpg" alt="placeholder" width="200" height="200" onClick="incorrectAnswer();">
<form id="answer_send" name="answer_send" action="form.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/>
</form>
</body>
</html>
The PHP
<?php
$id=$_POST['answer_id'];
echo $id;
?>
Note: Your AREA element is not going to work unless you assign it to something.
Your JavaScript function must be within tags or you'll receive errors.
The HTML/PHP part of this is working fine... You are experiencing these issues due to the following three lines...
document.getElementById("incorrectVid").style.display="block";
document.getElementById("incorrectVid").play();
document.getElementById("answer_id").value=id;

where is your name attribute ?
Every input must have a name attribute to retrieve it on php
and your html code must look like this:
<form id="answer_send" name="answer_send" action="answersend.php" method="POST">
<input type="hidden" id="answer_id" name="answer_id" value="10"/> </form>

Related

I can't transport HTML information to PHP

My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>

Unable to echo user queries in results

I am learning PHP and in the process of making a search engine. The following code will not echo the users input, in the search field, on the results page.
The search page:
<!DOCTYPE html>
<html>
<head>
<title>Search Engine</title>
<style type="text/css">
body {
background:#F0FFFF;
margin:-80px;
}
form {
margin:25%;
}
</style>
</head>
<body>
<form action="result.php" method="post">
<input type="text" name="user_query" size="80" placeholder="Enter Search Here" />
<input type="submit" name="search" value="Search Now" />
</body>
</html>
The results page:
<!DOCTYPE html>
<html>
<head>
<title>Results</title>
<style type="text/css">
.results {
margin:5%;
}
</style>
</head>
<body bgcolor="#F0FFFF">
<form action="result.php" method="get">
<span><b>Enter Query Here:</b></span>
<input type="text" name="user_keyword" size="80" />
<input type="submit" name="result" value="Search Now" />
</form>
<?php
if(isset($_GET['search'])) {
$get_value = $_GET['user_query'];
echo "<div class='results'>
$get_value;
</div>";
}
?>
</body>
</html>
Can someone tell me why the user's input is not being echoed when the search is run?
If you using method=post
In form tag you must receive you data as $_POST
Also method=get use $_GET
Edit this lines in result page to this
<?php
if(isset($_POST['search'])){
$get_value = $_POST['user_query'];
echo "<div class='results'>{$get_value}</div>";
}
?>
well the problem lies in your form tag you should have used method="get" if you want to get some data from database and if you are using method="post" you should have to use $_POST['search'] so your form tag shuld have to be like this
<form action="result.php" method="get">
and if you are going to use method="post" anyway than your $_GET['search'] should have to be $_POST['search']
but i will suggest you to use method="get" for searching query from database

Linking html to php with $_POST

I have been trying to create a form that reads a post from an HTML form and displays an element from that post IF it detects that the post exists.
However, each time the post is submitted, it simply reloads the form as though no post were provided.
<!DOCTYPE html>
<html>
<head>
<title>Upload from Manifest</title>
</head>
<body>
<?php
if (isset($_POST['manifest'])) {
echo 'we are in the IF';
echo($_POST['manifest']);
}
?>
<h1>Submission from manifest into main db</h1>
<div class="container offset-top120">
<form method="post" action="https://nhsggc.cogiva.com/prism/loadFromManifest.php" enctype="multipart/form-data">
<input id="manifest" type="text" />
<input id="submit" value="Submit" type = "submit" />
</form>
</div>
</body>
</html>
Your form is going to either a different page (https://nhsggc.cogiva.com/prism/loadFromManifest.php so check for that first) if you wanted it to go to same page, you can give the action as just '#', or put in the whole URL like you have.
You're missing the name attribute from your submit input and text input. Read up on the name attribute!
<input id="manifest" type="text" name="manifest">
<input id="submit" value="Submit" type="submit" name='submit' />
Then your PHP should look like this:
<?php
if (isset($_POST['submit'])) {
echo 'Inside an if';
echo $_POST['manifest'];
}
Then it should work.

onclick input type submit

In a form I have an <iframe> that contains a PHP file (editor.php). This PHP file contains an HTML form.
Well, when I do a "submit form", I call the same PHP file, for example main.php.
When I press the submit button, I have "onclick method" that it calls a Javascript function inside editor.php. This function executes the form.
My problem is that main form is executed correctly but the second form is not.
In the second loop of the form of editor.php receives nothing.
**Check this way it will work as you expected**
//main.php
<html>
<head>
<script type="text/javascript">
function validateMain()
{
alert('main');
}
function validateSub()
{
alert('sub');
}
</script>
</head>
<body>
<form id="main" onsubmit="return validateMain();">
<input type="text" name="first" id="first"/>
<input type="submit" value="Submit Main"/>
</form>
<iframe name="ifr-form" id="ifr-form" src="test.html"></iframe>
</body>
</html>
//test.html the second form included through iframe
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<form id="sub" onclick="return window.top.validateSub();">
<input type="text" name="first" id="second"/>
<input type="button" value="Submit Sub" />
</form>
</div>
</body>
</html>
you must check if php and iframe are compatible, as far as i Know I don't think that frames and php gives any output, hope this helps.

form updated by javascript missing post values

I have a form that I ad an element to via JavaScript before it is submitted.
parent.document.getElementById('submitcomment' + id).innerHTML = '<img class="image_real" src="/images/site.png" alt="Mostly Dirty," />
<input class="real" name="freshness" type="text" size="5" maxlength="6" />
<a href="#" onClick="submitComment('+[id]+'); return false;">
<img id="submitcommentimg<?php echo $id; ?>" src="images/check.png" alt="Comment!" border="0"></a>
<div class="submitcommentalert" id="comment_alert_<?php echo $id; ?>" style="display:none">Comment Posted!</div>';
When the form is submitted it seems to be missing the 'freshness' <input> element
That is if I try to access $_POST['freshness']; it is empty.
There is no value attribute in the freshness, this might be the issue. Have you checked to see of the $_POST['freshness'] even exists (isset())?
When the above code is executed, does the DOM gets updated with the newly added content? Also check with FireBug what's actually being posted to the server. Also you are using parent.document - could there be some iframe issues?
UPDATE:
Trying to reproduce the problem I've come up with this snippet which works as expected (when you submit the form, the value of the freshness input is correctly passed):
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function addDynamicContent() {
document.getElementById('myForm').innerHTML =
'<input name="freshness" type="text" />' +
'Submit';
}
function submitComment() {
document.getElementById('myForm').submit();
return false;
}
</script>
</head>
<body>
<input type="button" value="Add dynamic content" onclick="addDynamicContent();" />
<form id="myForm"></form>
</body>
</html>
Now you could tweak it to your needs.

Categories