Edit: I fixed the problem by just starting from scratch. Sorry to waste y'alls time. Please vote to close if you feel so inclined.
I'm having trouble getting the actual data in a form to submit when the input fields are added via javascript. The fields show up, and in the right place, but when I do a var_dump( fieldname) on the server side, nothing is showing up. Inspecting with Live HTTP headers tells me that the browser isn't trying to submit the dynamically added form fields.
Do I need to somehow "attach" my dynamically created form inputs to the form?
My code:
HTML
<form id="att_form" method="post" name="add_attachments" enctype="multipart/form-data" action="#">
<-- below input to prove form submits, just not the dyn added elements -->
<input name="data[one]" type="text" />
<div id="add_fields"></div>
<input type="submit" />
</form>
Javascript
function addFormField()
{
var id = 1;
var htm = "<p id='row" + id + "'><input type='text' size='20' name='txt[]' id='txt" + id + "' /></p>";
$("#add_fields".append( htm );
}
When I submit the form, my input named data[one] shows up as a POSTd value, but those added with addFormField() do not. They show up, in the HTML, at the correct place, but don't POST. Do I need to append the element as a child to my form element to get it to submit?
I've seen Submit form input fields added with javascript which is where I got the idea of appending the data specifically to the form child, but that will require some restructuring of my CSS. So, I'm hoping that it's something simple I can do in the above code.
Thanks in advance!
edit: fixed the typos, but still not working. I've decided to use the library free JS method discussed in the SO link above, as that works fine. Thanks for the help though!
there is 2 typos in your code:
htm instead of html
and add_fields / add_files
There were a lot of typos in your code. For example, you didn't close the selector tag for jquery.
You put
$("#add_fields".append( htm );
Should have been
$("#add_fields").append( htm );
Notice the missing parantheses.
But I believe your problem lies mainly in how you're trying to access the values through PHP. I just put your source in a test page and it all works if you access the values correctly.
<?php
foreach ($_REQUEST['txt'] as $printme)
echo $printme."<br/>";
?>
The above source works fine.
When you are adding/appending or using innerHtml to place form fields as html, then sometime it will place it outside the form, you will see it as part of form but internally it is not.
To resolve this issue you need to add form attribute with input filed like form=myForm, and myForm should be your form id.
Is that your actual code?
If so, you didn't close the input tag and the p tag.. so maybe the form ignore them..
var htm = "<p id='row" + id + "'><input type='text' size='20' name='txt[]' id='txt" + id + "' /></p>";
Related
I am new here and also don't know PHP.
I have got a project in which there is a table containing a Buy button in one cell and its value in another cell.
I want it like that, when the user clicks on "BUY", it's value gets passed to a contact form.
Like for example: ?buyrate={value from table cell}
I have got the form working to receive the value from the URL.
Eg: ?buyrate=25 . The form field gets the value 25.
But i dont know how to get the value from the cell, so that the admin can be able to change the Value and doesn't need to change the Button URL every time.
Please help anyone.
Thanks
Since you are already getting buyrate from the URL (I assume from a $_GET ie $buyrate = $_GET['buyrate'];), why don't you simply use that value in your button code, like such:
<input type="submit" name="buyrate" value="<?php echo $buyrate; ?>" />
Without seeing code, we're not sure what else you want to submit, but this should pass that value to whatever your form action is.
you need to send data via jquery to contact form like
//include jquery.js here
<script>
$(document).ready(function(){
$('#but').click(function(){
var val=$('#v').html();
window.location.href="somepage.php?buyrate="+val;
});
});
</script>
<table>
<tr><td id="but"><button value="Buy" /></td></tr>
<tr><td id="v">60</td></tr>
</table>
//this is just an example you can modify the code as per your conditions and requirements.
Im trying to create a grid on my page in each cell there will be a simple one line form. If a person enters data into lets say FieldA I would like the php to perform actionA but if the data was entered in FieldF I would like actionF performed. Is this possible without having to create a php for each cell and upload all those php files?
Or is there a way to perform the GET method in each form to append the data to the end of the action url without the field name showing (ie sample.com/somestuff/fieldA instead of sample.com/somestuff/fieldname=fieldA) thus not needing php at all?
Did you try anything. Please try to write some code. If you get struck paste the code here, somebody will help you out..
In my opinion, why you need different forms. Just have a form which has n text boxes and perform the task that you need.
Your problem is a little ambiguous to me. I'll give it a shot though.
On the form I would set:
`method="post" action="<?php echo $PHP_SELF ?>"`
This will cause the form to submit back to itself. Then at the top of the page you could do something like the following:
<?php
if (isset($_POST["fieldA"]){
performActionA();
} else if (isset($_POST["fieldB"]){
performActionB();
}
etc...
?>
Is that what you are trying to do? Keep in mind php is executed server side before any interaction with the user.
otherwise you could use javascript to change the action field of the form. (Untested)
<script type="text/javascript">
function setAction(elt){
var page = document.getElementById(elt).value;
document.myform.action="sample.com/somestuff/"+page;
}
</script>
<form id="myform" action="sample.com/somestuff/">
<input type="text" name="text1" onchange="setAction('text1')" />
</form>
We're using PHP to build a product page for a gallery website that's using GetSimple 3.0 CMS. We are trying to create a contact form that is displayed when you click a button. By default the contact form is in a DIV that's set to display: none. When you click the button it displays: block. When a user clicks the submit button for the form and calls the action it loads the contact.php file and resets the DIV to display: none resulting in the user not seeing the conformation text that their form was submitted. You can only see it by clicking on the contact button again and displaying that DIV to block manually.
We'd like the contact form DIV to persist after the submit button is clicked. I don't think showing our code would be helpful. We're just trying to find a way to implement this idea if possible.
At present our website is still early in it's development stage and it's still being hosted locally.
Thanks for any help.
use ajax submission of that particular form so that focus will not loss even the page will not be refreshed
If you can edit PHP code used for building the page, you can always display the style dynamically depending on the request, like this:
<div style="display:<?php echo (isset($_REQUEST['answer'])) ? 'block' : 'none';?>>
Thank you!
</div>
<form action="contact.php" method="post">
<input type="hidden" name="answer" />
...
</form>
You can also submit a form without reloading the page, e.g. with JQuery.Forms
If both the form and the PHP file are located in Contact.php, you could consider using this:
<div id="formWarp" <? if($_POST["contact"] == "true"){ echo 'style="display:none" '; } ?>>
<form method="POST" action="Contact.php">
<input type="text" name="something"/>
<input type="hidden" name="contact" value="true"/>
</form>
You can include any other <input>'s inside the form.
EDIT: Completely changed to reflect new information obtained to more directly address the problem.
Reading your above comments I'm fairly confident we are looking at the wrong function to edit. The wrapper element is not present in the function you posted which is what needs to have it's display toggled.
once you find that block of code we can edit it to look for the presence of post data which would indicate that the page is being loaded after a form submission has occurred (I can't promise that this won't cause it to trigger at the wrong time if there is more than one form on this page) and write in a style attribute to overwrite the default value if tested true.
Some where in that plugin there will be something defining the forms wrapper element
$html = '<div id="some-unique-id" class="some-class-name">';
You can break up this line and put a test for post data adding an inline style attribute if found
$html = '<div id="some-unique-id" class="some-class-name"'; //tag left open
if(isset($_POST) && (count($_POST))){ //Some Post Data Exists
$html .= ' style="display:block;"'; //Add display overwrite
}//Some Post Data Exists
$html .= '>'; //close the tag
The test we are performing here is if(isset($_POST) && (count($_POST))){ which is checking to make sure that A) $_POST exists and B) it has at least one element (this is using type juggling to convert a numeric result from count() into its Boolean equivalent (where anything greater than 0 will test true)
Now, as i mentioned, there may be more than one form on this page and it is possible it will be displayed afterwards which would auto show your contact form when you don't want to have it showing. Based on the example you provided in a comment it looks like this function exists within a class. If the block of code we are looking for exists within the same class it might be possible to leverage the id attribute to restrict the check to post data from the form you are interested.
$html = '<div id="some-unique-id" class="some-class-name"';
//Check for contact form specific post data
//(if $this->id is within scope and still the same)
if(isset($_POST) && (isset($_POST['p01-contact' . $this->id]))){
$html .= ' style="display:block;"'; //Add display overwrite
}//Post Data Exists
$html .= '>';
In this test we are hoping that $this exists and the attribute id of $this is the same as it was at the time that the form was originally drawn so we can look for post data that is related to the specific contact form. (the name we are looking for is based off of the code example you posted as a comment)
Unfortunately without looking at the source of the site/plugin it will be impossible for me to tell you where you can find what you are looking for. Once you find it though one of these two scenarios should hopefully take care of your problem.
Is this a publicly available plugin we could look at or something developed in-house?
In PHP, in a particular CMS I am using a custom field, which works like google suggest.
As in, for each letter I type an SQL query is performed and matching records are displayed. When clicking on a record it fills the field with that record.
I am fairly certain this is all done with JavaScript.
I need to know how I can access the resultant content of that field, with the text placed through JS, before it is submitted so I can explode() it.
The CMS I am using is using mootools, so a solution relying on mootools would be ideal.
(This answer assumes that you have control over the markup of your forms (the form that requires a string "explosion" before submit) and/or you feel comfortable tinkering with whatever plugins you're using.)
first, make sure that you aren't submitting your form using an actual submit button (). We'll need to submit the form using javascript after fiddling with the field's contents.
next, make sure that your input box (the one you're grabbing text from) and your hidden inputs have unique ids. This will make it easier to query the DOM for the data we need.
Inside your form, in place of a "real" submit button, create a form button:
<form action="something.php" name="myform">
<input type="hidden" id="hiddenItem">
// SOME STUFF
<input type="text" id="autocomplete_field" value="whatever"/>
// SOME OTHER STUFF
<input type="button" value="Submit" onclick="processForm(this)"/>
</form>
Then, write a javascript function to process the string and submit the form:
processForm = function(el){
text = $('autocomplete_field').get('value');
// Lets assume the strings separates words (what you're exploding apart) using spaces
// something like 'DOGS CATS BIRDS PETS'
var array = text.split(' ');
// returns ['DOGS','CATS','BIRDS','PETS']
$('hiddenItem').set('value',array[0]);
// #hiddenItem now has the value 'dogs'
//SUBMIT THE FORM
el.getParent('form').submit();
};
Hope this helps!
You could try to use JS to send the field on some event (onkeyup?) to your php script. After it does it's part, store the result as a session variable and you can retrieve that later.
Try using jquery's get function.
Was that your question?
I have a database which holds the residents of each house in a certain street. I have a 'house view' php web page which can display an individual house and residents when given the house number using 'post'. I also have a 'street view' web page which gives a list of houses. What I want to know is if you can have links on the street view which will link to the house view and post the house number at the same time without setting up a form for each?
Regards
If you want to pass the data using POST instead of GET, you can do it using a combination of PHP and JavaScript, like this:
function formSubmit(house_number)
{
document.forms[0].house_number.value = house_number;
document.forms[0].submit();
}
Then in PHP you loop through the house-numbers, and create links to the JavaScript function, like this:
<form action="house.php" method="POST">
<input type="hidden" name="house_number" value="-1">
<?php
foreach ($houses as $id => name)
{
echo "$name\n";
}
?>
</form>
That way you just have one form whose hidden variable(s) get modified according to which link you click on. Then JavaScript submits the form.
I assume that each house is stored in its own table and has an 'id' field, e.g house id. So when you loop through the houses and display them, you could do something like this:
<a href="house.php?id=<?php echo $house_id;?>">
<?php echo $house_name;?>
</a>
Then in house.php, you would get the house id using $_GET['id'], validate it using is_numeric() and then display its info.
You cannot make POST HTTP Requests by some_script
Just open your house.php, find in it where you have $house = $_POST['houseVar'] and change it to:
isset($_POST['houseVar']) ? $house = $_POST['houseVar'] : $house = $_GET['houseVar']
And in the streeview.php make links like that:
Or something else. I just don't know your files and what inside it.
This is an old thread but just in case anyone does come across i think the most direct solution is to use CSS to make a traditional form look like an anchor-link.
#ben is correct you can use php and javascript to send a post with a link, but lets ask what the js does -- essentially it creates a form with style='display:none' sets an input/text line with value='something' and then submits it.
however you can skip all this by making a form. setting style='display:none' on the input/text lines (not the form itself as above) and then using CSS to make the button look like a normal link.
here is an example is i use:
in PHP Class,
public function styleButton($style,$text){
$html_str = "<form id='view_form' action='".$_SERVER['REQUEST_URI']."' method='post' >";
$html_str .= "<input style='display:none;' name='list_style' type='text' value='".$style."' >";
$html_str .= "<input id='view_button' type='submit' value='".$text."' >";
$html_str .= "</form>";
return $html_str;
}
Then in the CSS id="view_form" set "display:inline;"
and in the CSS id="view_button" set to something like: "background:none;border:none;color:#fff;cursor:pointer"
I would just use a value in the querystring to pass the required information to the next page.
We should make everything easier for everyone because you can simply combine JS to PHP
Combining PHP and JS is pretty easy.
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = $house_number; document.forms[0].submit();</script>";
Or a somewhat safer way
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = " . $house_number . "; document.forms[0].submit();</script>";
This post was helpful for my project hence I thought of sharing my experience as well.
The essential thing to note is that the POST request is possible only with a form.
I had a similar requirement as I was trying to render a page with ejs. I needed to render a navigation with a list of items that would essentially be hyperlinks and when user selects any one of them, the server responds with appropriate information.
so I basically created each of the navigation items as a form using a loop as follows:
<ul>
begin loop...
<li>
<form action="/" method="post">
<input type="hidden" name="country" value="India"/>
<button type="submit" name="button">India</button>
</form>
</li>
end loop.
</ul>
what it did is to create a form with hidden input with a value assigned same as the text on the button.
So the end user will see only text from the button and when clicked, will send a post request to the server.
Note that the value parameter of the input box and the Button text are exactly same and were values passed using ejs that I have not shown in this example above to keep the code simple.
here is a screen shot of the navigation...
enter image description here