GET/POST variable not set in netbeans IDE - netbeans

I dunno if its a silly mistake but a post/get variable is not being set, while it is supposed to be. Here are the HTML and php code snippets:
<html>
<head>
<title>
Chain Story
</title>
</head>
<body>
<form method="GET" action="check-valid.php">
<textarea name="a" rows="5" cols="50"></textarea>
<input type="submit" value="Add" />
</form>
</body>
</html>
check-valid.php:
<?php
require 'includes/connect.inc.php';
$conn_ref = connect_db('chainstory') or die(mysqli_error());
if(isset($_GET)){
echo 'Get variable set';
if(isset ($_GET['a'])){
$as = $_GET['a'];
$query = "insert into story1 values (1, " . $as . ")";
mysql_query($query, $conn_ref);
}
else{
echo $_GET;
}}
?>
I get the following output:
Get variable set
Notice: Array to string conversion in /home/kevin/Code/php/myWebsite/check-valid.php on line 15
Array
I am coding this in netbeans. Can anyone point me out what mistake im making? :(

Did you try rename the textarea? Longer name , and give id to textarea same the name.
What browser does you use for testing?
I met some problems with input names in IE for example if an input name matches a javascript function name or protected names. Have you a javascript function or variable in your code what's name is a ? Because if the name of the input conflicts with a js var or name IE does not send the input field to the server. (Chrome Firefox and other browsers does)

Related

Files Array is not arriving at upload.php

i've got the following form
<form action="http://test.mysideurl.de/upload.php" method="post" enctype="multipart/form-data" target="upload_target" id="MyUploadForm" >
<input name="file[]" type="file" class="file" multiple>
This form should send multiple files (if selected) to upload.php which looks like follows:
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name){
$file_name = $key.$_FILES['file']['name'][$key];
$tmp_name = $key.$_FILES['file']['tmp_name'][$key];
$doc_id=process_image($file_name,$tmp_name,$doc_id);
}
console of firefox says:
Warning: Invalid argument supplied for foreach() in ...myurl... on line 15
Has anyone an idea, why it doesn't receive the files array?
Best regards Daniel

PHP 5.3 write contents to plain text

I am trying to make a PHP script write into a plain text file. I have done this before and it worked just fine. But it's not working this time for some reason.
Here is the HTML I am using:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/css/feedback.css" >
<title>Questions, Comments, Suggestions</title>
</head>
<body>
<p class="title">Questions, comments, and suggestions here!</p>
<form method="post" name="userFeedback" action="/submit.php">
<textarea id="comments" placeholder="Leave a comment or review here..."></textarea>
<textarea id="name" placeholder="Your name here"></textarea>
<textarea id="contact" placeholder="Put any means of contact you want to here (optional)"></textarea>
<br>
<input class="enter" type="submit" value="Enter">
</form>
</body>
</html>
All I want to do with this is to print out whatever is entered onto a plain .txt file with PHP 5.3. Here is the code:
$data = ($_POST["comments"] ." || ". $_POST["name"] ." || ". $_POST["contact"]);
$data = strip_tags($data);
$file = "feedback.txt";
$f = fopen($file, "a+");
fwrite($f, $data . "\n" . "\n");
fclose($f);
header ( 'Location: index.html' );
Please remember that I am using 5.3. I'm sure there's a simple error in here somewhere. Can someone help me with this? Thank you in advance!
We got it! Turns out that the PHP $_POST method looks for the "name" attribute and not the "id".

Is there any way possible to apply form requirements to a drop down field in a PHP contact form?

I would like to create a requirement that if nothing is selected from a drop down field in my contact form that a message will come up saying "Please choose", and the form will not be able to be submitted unless something is chosen. I have gotten requirements to work on all of my text input forms, but cannot figure out how to create one for the drop down field.
The drop down HTML looks like this:
<div class='container'>
<label for='destemail' > Which department are you trying to reach?*</br> You must select a department.</label></br>
<select name="destemail" id="destemail">
<?php foreach ($emailAddresses as $name => $email) { ?>
<option value="<?php echo htmlspecialchars($name); ?>"><?php echo htmlspecialchars($name) ; ?></option>
<?php } ?></select>
<span id='contactus_destemail_errorloc' class='error'></span>
</div>
I got the other form requirements to work like so:
The HTML -
<div class='container'>
<label for='name' >Your Full Name*: </label><br/>
<input type='text' name='name' id='name' value='<?php echo $formproc->SafeDisplay('name') ?>' maxlength="50" /><br/>
<span id='contactus_name_errorloc' class='error'></span>
</div>
The Javascript -
<script type='text/javascript'>
<![CDATA[
var frmvalidator = new Validator("contactus");
frmvalidator.EnableOnPageErrorDisplay();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("name","req","Please provide your name");
</script>
The PHP -
//name validations
if(empty($_POST['name']))
{
$this->add_error("Please provide your name");
$ret = false;
}
I tried the exact same coding for the drop down but with the different id names where appropriate, and it didn't work. Why doesn't this same method work for the drop down?
Help much appreciated!
I can't see what the Validator() code is doing, but you can just check to see whether the select field is empty using Javascript or jQuery.
jQuery way:
if( !$('#destemail').val() ) {
alert('Empty');
}
The problem may lie in that your select box actually does have a value, which is whatever the first value printed out in it is. The Validation function may be checking for any value, and since the select does have one, it returns as valid.
You could set up a default value to show first, something like "Please select a Department", and then do the jquery/javascript check for that text. If it exists, then you know an option has not been selected.

Submit the value of a <p> element when an html form is submitted

I have this code: <p class = "foo">Text</p>
And I also have a form: <form action = "XXX.php" method = post></form>
However, how can I get the value of the <p> when I submit it, as the <p> element can be changed.
So what I mean is to be able to post the value of the <p> when the user submits the form, and to be able to access it from that php file with: $_POST['foo'];
Thanks, I have tried to be as clear as possible.
You have to use Javascript for that
A jQuery function that will work
$("form").submit(function(){
var value = $("p").html();
// If foo already exists
if( $("[name=foo]").length > 0 )
{
$("[name=foo]").val(value);
}
else
{
var input = $("<input />", { name : "foo",
value : value ,
type : "hidden" });
$(this).append(input);
}
});
Use
<input type="hidden" value="something" name="something" id="something" />
and when you change inner html of <p> change the value of hidden input.
I think your best bet is to make it an input with readonly enabled, and style to to look like a <p>. It's better then trying to add it to the POST parameters with JavaScript.
Here's a quick example. I bet it could still be improved with a few extra CSS quirks, experiment a bit.
The easiest thing to do is set the value of a hidden form field when you change the contents of your <p>.
Alternatively, you can get its contents and post with JavaScript.
For text you need to use input field:
<input type="text"/>
Form fields should must have an id:
<input type="text" id="pewpew" class="foo"/>
I would go with:
<input type="text" id="pewpew" class="foo" value="default text goes here"/>
OR
Go with different workarounds, like setting form's hidden elements on the fly, etc.
You can create hidden field on the fly and set its value on form submit. Like this:
<form id="form" action="/somewhere" method="post">
<p>Some text</p>
<input type="submit" value="Submit"/>
</form>
<script type="text/javascript">
var form = document.getElementById('form');
form.onsubmit = function()
{
var p = this.getElementsByTagName('p')[0];
if (!document.getElementById('pval'))
{
var pinput = document.createElement('input');
pinput.setAttribute('type', 'hidden');
pinput.setAttribute('id', 'pval');
pinput.setAttribute('name', 'p');
this.appendChild(pinput);
}
document.getElementById('pval').value = p.innerHTML;
return true;
}
</script>
Works, i've tested.

How to execute some code before posting a form using classic ASP

Using classic ASP, I want to execute some code before submitting to a form, below is my code.
I am trying to store login credentials into cookies before posting the form to another page.
Please note that I am posting to an ASP.NET page that is held in a separate domain.
Another example, just to make this clearer. Let's say I want to validate fields before posting without using JavaScript.
<%
dim uname
uname= Request.Cookies("username")
%>
<FORM action="httppost_login.aspx" METHOD ="post" >
<br />Username: <INPUT NAME ="username" SIZE ="30" maxlength="15" value="<% if uname <>"" then response.write(uname) %>" />
<br />Password: <INPUT NAME ="password" SIZE ="30" type=password maxlength="20" />
<br />
<INPUT TYPE ="SUBMIT" value="Login" name="btnSubmit" />
<input type="checkbox" name="remember" value="Remember my username" <%if uname <> "" then Response.Write("checked")%> />Remember my username
</FORM>
<%
If Request.Form("btnSubmit") <> "" Then
uname=Request.Form("username")
dim rememberme
rememberme = request.form("remember")
if rememberme <> "" and uname <> "" then
Response.Cookies("username") = uname
Response.Cookies("username").Expires = Date() + 30
end if
end if
%>
You can't run code easilly after the submit button is pressed.
This comes down to design. Your form action is:
httppost_login.aspx
The 'standard' way of achieving what you want is by having code on that page which handles the cookies/anything else.
I would usually give the form action a querystring:
<FORM action="httppost_login.aspx?action=login" METHOD ="post" >
Then on that page, have:
Dim strPageAction
strPageAction = request.querystring("action")
if(strPageAction = "login" then
'Put your cookie code here, request.form("username") etc will still work!
end if
yes, a redirect page can post to a third party page using xmlhttp