use only input for change image without submit - php

I want use that form for change image but without click to submit.
<form action="chav.php" method="POST" enctype="multipart/form-data" >
<label for="fileup"><img src="/images/img-ico.png"/>Aggiungi Foto</label>
<input type="file" name="fileup" id="fileup" /><br/>
<input type="submit" name='submit' value="Upload" />
<input type="hidden" value="<?php echo milldigital_filter('get',$_REQUEST['pg']); ?>" name="pg" />
</form>
so when i select image, directly submit without press any button.

You can submit form using javascript within form event attribute onchange:
...
<input type="file" name="fileup" id="fileup" onchange="this.form.submit()" />
...
When browser render form element with onchange property it starts to observe if its value changes and execute javascript code when it does. It changes when you select file. documentation
this.form means we'll refer to form object that this html (form) element belongs to. Then execute its submit() method.

Related

passing values from one form to another form with in the same page in php

I have 2 forms named form1 and form2 in a single php page.form1 contains one text box.form2 contains two text boxes with a submit button.I want to validate all the textboxes when ever submit button pressed.But I cannot access the textbox value in form 1.My code is given below.
<html><body>
<form name=form1 method=POST>
<input type=text name=f1_t1>
</form>
<form name=form2 method=POST>
<input type=text name=f2_t1>
<input type=submit name=sub1>
</form></body></html>
<?php
if(isset($_POST['sub1']))
{
$name=$_POST['f1_t1'];
echo $name;}
?>
this code error as undefined variable f1_t1.Can anyone help please?
I suggest putting everything in a single form, and displaying/hiding sections of it as necessary (for example, with jQuery and CSS).
As an example, you could do this:
<form>
<div id="part1">
<input name="t1" type="radio" value="v1" />
<input name="t1" type="radio" value="v2" />
</div>
<div id="part2" style="display: none;">
<input name="t2" type="text" />
<input type="submit" />
</div>
</form>
Only the fields in one form are submitted.
If for some reason you need to get the falues from a second form, you need to do that before posting, that is, on the client side. You could for example use javaScript to read the value of the field from the first form, write it to a hidden field in the second, and then post that to the server.

Php get method resseting url

i am submitting form using get method to a url which already contain parameter like
localhost/myfile.php?section=console
my form code is
<form method="GET" action="<?=basename($_SERVER['PHP_SELF'])?>/section=console">
<input type="text" name="cmd" />
<input type="submit" value="execute" />
</form>
when i submit this data through post type it submit data then it submit like myfile.php?cmd=blahblah
but i want to submit it to myfile.php?section=console&cmd=blahblah.
i can do this by using hidden field but i am insearch of other better way
Simply add a hidden field into your form
<input type="hidden" name="section" value="console">
If you are not interested in using hidden fields then rewrite your form like this
<form method="GET" action="<?php basename($_SERVER['PHP_SELF']) ?>/?section=console">
You should use a hidden input within your form
<form method="GET" action="<?php echo basename($_SERVER['PHP_SELF']); ?>">
<input type="hidden" name="section" value="console">
<input type="text" name="cmd">
<input type="submit" value="execute">
</form>

How to trigger file input in repeating form

I have a div with dynamic repeating form to change image of a repeating div based on respective id, when a div image is clicked i want to trigger file input for clicked div.
contents are loaded into div(image-container) with jquery post request.
HTML
<div id="image-container"></div>
//repating dynamic form
<?php while($row=mysqli_fetch_assoc($result)):?>
<div class="image">
<form name="image_change">
<img src=".." class="image_change"/>
<input name="image" type="file" />
<input name="id" type="hidden" />
<input name="edit" type="submit" />
</form>
</div>
<?php endwhile;?>
jQuery
$('#image-container').on('click', 'form[name=image_change] .image_change', function(){
$(this).find('input[name=image]').trigger('click');
return false;
});
This works only when form is not repeating, how can i make it work with repeating form.
Please see and suggest any possible way to do this.
Thanks.
This is because you have multiple forms with the same name and only the first one is recognized.
Remove name attribute from the form:
<div class="image">
<form>
<img src=".." class="image_change"/>
<input name="image" type="file" />
<input name="id" type="hidden" />
<input name="edit" type="submit" />
</form>
</div>
and modify the js code to the following
$(document).on('click', 'form .image_change', function(){
$(this).parent().find('input[name=image]').trigger('click');
return false;
});
if you need to differentiate the form add ids to the forms.

add the value of javascript in the hidden field

I have this javascript line
<script type="text/javascript">document.write(top.location);</script>
and this code
<form action="rating.php" method="post">
<input type="hidden" name="url" value="xxxx" />
<input type="submit" />
</form>
<?
echo $_POST["url"];
?>
Please, How i can add the value of javascript (xxx) in the hidden field?
Also does it possible to let <input type="submit" />have automatic submit ?
Thanks
Do you mean this, in order to feed the hidden field?
<input type="hidden" name="url" value="<? echo $_POST["url"]; ?>" />
Regarding submit, you can use javascript to handle the submit event:
If you add the attribute name="FORMNAME" inside the <form ... > tag, you can use:
document.FORMNAME.submit();
for submitting the form, without using the submit button.
In order to feed a hidden field through javascript, use:
document.FORMNAME.url.value = top.location;

how to pass values from one page to another on jquery form submit

I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.

Categories