Can we define name as array in submit button? - javascript

Can we define name of a submit button as list of array??. If yes then if S1 was clicked the values of the text input should be transfer to one of the input box provided in new div and if S2 was clicked then value should be go below the previous input box. and number of input box in new div depend upon the number of submit button.
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
name="Main" method="POST">
<input type="text" name="p1[]" value="">
<input type="text" name="p1[]" value="">
<button type="submit" name="S[]">S1</button>
<button type="submit" name="S[]">S2</button>
<button type="submit" name="S[]">S3</button>
</form>
<?php
if (isset($_REQUEST['submit'])) {
foreach($_POST['p1'] as $i)
{
echo "Hobbies are :".$i."<br>";
}
}
?>
Not getting any output from this.

Related

Onclick copy input text to another location

I am making a admin panel. When users input data into a text field i want to copy the data in the text field to another text field.
My problem is the code works until i added a insert into database function to the same button.
How do i get the button to perform both functions. is there a better way to do the onclick function. As i am using two text fields but i really want one to be the product title. Like in the image below:
Code is
<?php
$add_product_errors = array();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// price validate - must be decimal(float)
if (empty($_POST['price']) || !filter_var($_POST['price'], FILTER_VALIDATE_FLOAT) || ($_POST['price'] <= 0)) {
$add_product_errors['price'] = "Please enter a product price";
}
// item name validate
if (empty($_POST['item_name'])) {
$add_product_errors['item_name'] = "Please enter a name";
}
// item name description
if (empty($_POST['desc'])) {
$add_product_errors['desc'] = "Please enter a product description";
}
//add to database
//if (empty($add_product_errors)) {
$q = 'INSERT INTO Product (Product_Name,Product_Desc,Product_Price) VALUES (?,?,?)';
$stmt = mysqli_prepare($dbc, $q);
//debugging
$stmt = mysqli_prepare($dbc, $q) or die(mysqli_error($dbc));
mysqli_stmt_bind_param($stmt, 'sss', $item_name, $desc, $price);
$item_name = strip_tags($_POST['item_name']);
$desc = strip_tags($_POST['desc']);
//100 - changes the way the decimal displays in database
$price = strip_tags($_POST['price'] * 100);
//execute the query
mysqli_stmt_execute($stmt);
printf("%d Item Added.\ ", mysqli_stmt_affected_rows($stmt) === 1); {
mysqli_stmt_close($stmt);
}
}
?>
HTML
<form name="product_form" enctype="multipart/form-data" action="admin_p.php" method="post" accept-charset="utf-8" >
<input type="textfield" id="title" name="title" value="">
<input type="textfield" id="item_name" name="item_name" placeholder="item name" value="" <?php $add_product_errors ?>/><br>
<textarea id="desc" name="desc" value="" placeholder="Item description" rows="3" maxlength="200" required ><?php $add_product_errors ?></textarea><br>
<input type="textfield" id="price" name="price" value="" placeholder="£" maxlength="30" required <?php $add_product_errors ?>/><br>
<input type="submit" name="add" value="add" value="add" class="btn" onclick="myFunction()">
<!-- copy item_name to page title -->
<script>
function myFunction() {
document.getElementById("title").value = document.getElementById("item_name").value;
}
</script>
Consider changing the submit input to a regular button so that you can control the control flow entirely. It might also make the control flow clearer for you to understand.
<form name="product_form" enctype="multipart/form-data" action="admin_p.php" method="post" accept-charset="utf-8" onsubmit="myFunction()">
... your HTML here ...
<input type="button" name="add" value="add" value="add" class="btn" onclick="myFunction()">
</form>
<script>
function myFunction() {
// Update your field value
document.getElementById("title").value = document.getElementById("item_name").value;
// Submit your form.
document.forms["product_form"].submit();
}
</script>
You can bind the copying of the text on the onsubmit event like this:
<form name="product_form" enctype="multipart/form-data" action="admin_p.php" method="post" accept-charset="utf-8" onsubmit="myFunction()">

fill random() into text field when submit is pressed

I have a text field in my form and a submit button. As soon as submit button is clicked, I would like to fill this text field value with random number by using Math.rand() but it did not work, please help:
Here is my form:
<form id='testForm' method="post" action="process.php">
<input type="text" id= "randnum" name="randnum" value="">
<input type ="submit" value="Submit">
</form>
Here is my javascript
$('#testForm').submit(function(e) {
document.getElementById('randnum').value = Math.random();
});
When form is submitted, The $_POST['randnum'] variable is still nothing? Please help...
Make value equal to random on input[type=submit] click, Then form will also get submitted.
$('input[type=submit]').click(function() {
document.getElementById('randnum').value = Math.random();
});
use input type button instead of submit.DEMO
<form id='testForm' method="post" action="process.php">
<input type="text" id= "randnum" name="randnum" value="">
<input type ="button" value="Submit" id="sbmt">
</form>
Use click event and then submit
$('#sbmt').click(function(e) {
document.getElementById('randnum').value = Math.random();
alert(document.getElementById('randnum').value);
$("#testForm").submit();
});
HTML :
<form id="testForm" method="post" action="process.php">
<input type="text" id= "randnum" name="randnum" value="">
<input type ="button" value="Submit">
</form>
Javascript :
$('#testForm').click(function(e) {
document.getElementById('randnum').value = Math.random();
});

HTML - Two forms with 'enter' keypress to know which form I am entering

I have two forms and I would like to make it easy to type in one form text field and press enter and the page knows what form is being filled out.
Form 1 (example: search):
<form action="" method="post" name="form1">
<input type="text" name="txt1" />
<input type="submit" value="Enter 1" />
</form>
Form 2 (example: login):
<form action="" method="post" name="form2">
<input type="text" name="txt2" />
<input type="submit" value="Enter 2" />
</form>
Both passes through a PHP script to validate and off to its correct site.
Search is added to a page that is included in every page (MVC) header and the login is on its own page but both come together in one page as two forms. When logging in on the login page I enter username and password and press enter but it defaults to the search submit button and would like to know its being entered on the login submit button.
Appreciate your help...
If you give your submit buttons a name, you will be able to detect them in PHP.
<input type="submit" name="submit" value="Enter 2" />
and later
if ($_POST['submit'] == 'Enter 2') // ...
Since you know the field names you're looking for in each form, you can key off of that:
<?php
if (isset($_POST['txt1']) {
// do one thing
} else {
// do the other
}
<form action="" method="post" name="form1">
<input type="hidden" name="form" value="1" />
<input type="text" name="txt1" />
<input type="submit" value="Enter 1" />
</form>
<form action="" method="post" name="form2">
<input type="hidden" name="form" value="2" />
<input type="text" name="txt2" />
<input type="submit" value="Enter 2" />
</form>
The intval() will validate the posted value.
if (intval($_POST['form']) == 1){}
elseif (intval($_POST['form']) == 2){}
Taken from this article: http://www.javascript-coder.com/html-form/html-form-submit.phtml#multiple
Multiple Submit buttons
You can have more than one submit buttons in a form. But, how to identify from the server side which of the buttons was pressed to submit the form?
One way is to have different names for the submit buttons.
<input type="submit" name="Insert" value="Insert">
<input type="submit" name="Update" value="Update">
In the server side script you can do a check like this :
if(!empty($_REQUEST['Update']))
{
//Do update here..
}
else
if(!empty($_REQUEST['Insert']))
{
//Do insert Here
}
The second method is to have different values for submit buttons with the same name.
<input type="submit" name="Operation" value="Insert">
<input type="submit" name="Operation" value="Update">
The server side code goes like this (PHP code):
if($_REQUEST['Operation'] == 'Update')
{
//Do update here..
}
else
if($_REQUEST['Operation'] == "Insert")
{
//Do insert here
}

pass input value to another form on a diff url and run submit wordpress

I have 2 forms, 1st form is to submit an input value to the next page, and on the next page there's the 2nd form which is a search map function.
the 1st form is displayed on the homepage
<form role="search-map" method="" id="search-map" action="/find">
<h4>Where To Buy</h4>
<input type="text" class="form-control" id="addressInput" name="addressInput" placeholder="Your Suburb or Postcode">
<button type="submit" class="btn btn-vc btn-default btn-lg" id="search-address-btn">Find</button>
</form>
the 2nd form is on another page which is /find that has a map search plugin
<form onsubmit="cslmap.searchLocations(); return false;" id="searchForm" action="">
<input type="text" id="addressInput" name="addressInput" placeholder="Your Suburb" size="50" value="where-i-want-value-to-be-pass">
<input type="submit" class="slp_ui_button" value="Find" id="addressSubmit">
</form>
any ideas how can i pass the value from the 1st form "addressInput" to the 2nd form? and the run the search on the 2nd form? here's the 2nd form btw
i tried searching here but what i need seems to be more complex that what i have found
or maybe how can i get the 2nd page to get the value from the url (?addressInput=North+Bega%2C+NSW%2C+2550) into the 2nd form input "addressInput" and run the submit button function when the page loads?
thanks in advance guys
I'm assuming you want to get the value from s to the second form.
replace where-i-want-value-to-be-pass
with <?php echo $_REQUEST['addressInput']; ?>
<form onsubmit="cslmap.searchLocations(); return false;" id="searchForm" action="">
<input type="text" id="addressInput" name="addressInput" placeholder="Your Suburb" size="50" value="<?php echo $_REQUEST['addressInput']; ?>">
<input type="submit" class="slp_ui_button" value="Find" id="addressSubmit">
</form>

Unset session variable on button click event

I can't unset a session variable properly using a "remove button" click. I have two pages: product page and shopping cart page, but problem is that I have a "remove button" in the shopping cart which is not working properly.
When I click on "remove button" it should run unset($_SESSION['product1'].
But when I select another item then it can display "previous session item" so it means remove button does not run unset($_SESSION['product1'] properly. How do I solve this issue?
Product Page
session_start();
$id=$_REQUEST['id'];
$_SESSION['pid1']= $_POST['ids']; //Product ID//
$_SESSION['product1'][]=$_POST['product'];
<form method="post">
<input type="hidden" name="product[]" value="<?php echo $row1['product']; ?>" />
<input type="hidden" name="ids" value="<?php echo $id?>" />
</form>
<input type="submit" class="button1" name="addtocart" value="Add To Cart"
/>
</form>
Shopping Cart Page
session_start();
$pid=$_SESSION['pid1'];
function remove_product($pid){
$pid=intval($pid);
$max=count($_SESSION['product1']);
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['product1'][$i]['pid']){
unset($_SESSION['product1'][$i]);
break;
}
}
$_SESSION['product1']=array_values($_SESSION['product1']);
}
if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
remove_product($_SESSION['product1'][$pid]);
}
<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
<input type="button" class="button2" value="Remove"
onclick="javascript:del(<?php echo $pid?>)"/>
Shopping Cart Page Javascript
<script language="javascript">
function del(pid){
if(confirm('Do you really mean to delete this item')){
document.form1.pid.value=pid;
document.form1.command.value='delete';
document.form1.submit();
}
}
function clear_cart(){
if(confirm('This will empty your shopping cart, continue?')){
document.form1.command.value='clear';
document.form1.submit();
}
}
</script>
It might be caused by
$pid=$_SESSION['pid1']; instead of $pid=$_POST['pid1'];
event that is tied to tag wrapped around a button, you coud just use
<input type="button" class="button2" value="Remove" onclick="del('<?php echo $pid?>');"/>

Categories