one form two actions with javascript - javascript

Trying to first populate fields with one js button and then submit that collect data with a second js button
I can get the fields to poulate, when I click the "Populate" button, but when I try to do the submit none of the values show on the ordertest.php
Thanks
<form action="#" name="data" id="data">
<input type='button' value='Populate' onclick='popContact()' /><BR>
<input type="submit" onclick="document.getElementById('data').action = 'ordertest.php'" value="Payment Submit" /><BR>
Contact Info:<BR>
First Name: <input type='text' readonly="readonly" name='cfname' /><BR>
Last Name: <input type='text' readonly="readonly" name='clname' /><BR>
Email:<input type='text' readonly="readonly" name='cemail' /><BR>
</form>
<script type="text/javascript">
function popContact()
{
var c = window.external.Contact;
data.cfname.value = c.FirstName;
data.clname.value = c.LastName;
data.cemail.value = c.EmailAddr;
}
</script>
---> ordertest.php
<?php
$current_firstname = $_POST["cfname"];
$current_lastname = $_POST["clname"];
$current_email_address = $_POST["cemail"];
echo "current_firstname ".$current_firstname;
echo "<br>";
echo "current_lastname ".$current_lastname;
echo "<br>";
echo "current_email_address ".$current_email_address;
?>
also tried
<input type="submit" onclick="this.form.action='ordertest.php'" value="Payment Submit" />

Oppps.
using this combination works, $_GET
<input type="submit" onclick="this.form.action='order.php'" method="POST" value="Payment Submit" />
---> ordertest.php
<?php
$current_firstname = $_GET["cfname"];
$current_lastname = $_GET["clname"];
$current_email_address = $_GET["cemail"];
echo "current_firstname ".$current_firstname;
echo "<br>";
echo "current_lastname ".$current_lastname;
echo "<br>";
echo "current_email_address ".$current_email_address;
?>

Related

JSon array increment my id value from $_POST['id']

In my php script, I have managed to get all the data from a form into a json database, however everytime I submit my form, the id value is always == 1. How would I be able to change this, and add more values to my id value, like value++? How would I be able to do that?
<?php
$message = '';
$error = '';
if(isset($_POST["submit"]))
{
if(empty($_POST["title"]))
{
$error = "<label class='text-danger'>Enter title</label>";
echo $error;
}
else if(empty($_POST["decs"]))
{
$error = "<label class='text-danger'>Enter Gender</label>";
echo $error;
}
else if(empty($_POST["cont"]))
{
$error = "<label class='text-danger'>Enter Designation</label>";
echo $error;
}
else
{
if(file_exists('postbl.json'))
{
$current_data = file_get_contents('postbl.json');
$array_data = json_decode($current_data, true);
$extra = array(
'id' => $_POST['id'],
'title' => $_POST['title'],
'decs' => $_POST["decs"],
'cont' => $_POST["cont"]
);
$array_data[] = $extra;
$final_data = json_encode($array_data);
if(file_put_contents('postbl.json', $final_data))
{
$message = "<label class='text-success'>File Appended Success fully</p>";
echo $message;
}
}
else
{
$error = 'JSON File not exits';
}
}
}
?>
my form is like this
<?php session_start();
if(!isset($_SESSION['loggedin'])) header("Location: session.php");
if($_SESSION['loggedin']===FALSE) header("Location: session.php");
?>
<!DOCTYPE html>
<html><head><title>Secret Area</title></head>
<body>
<form action="/admin/form_process.php" method="POST">
<br />
<input type="hidden" name="id" value="1" />
<label>Title</label>
<input type="text" name="title" class="form-control" /><br />
<label>Description</label>
<input type="text" name="decs" class="form-control" /><br />
<label>Content</label>
<input type="text" name="cont" class="form-control" /><br />
<input type="submit" name="submit" value="Append" class="btn btn-info" /><br />
<?php
if(isset($message))
{
echo $message;
}
?>
</form>
<form action="logout.php" method="POST">
<input type="submit" name="logout" value="Log out">
</form>
<p>© 2017 Blog Message</p>
</body>
</html>
Your problem is here
<input type="hidden" name="id" value="1" />
Notice how the 1 is set as value.
Change the 1 with a variable. Something like
<input type="hidden" name="id" value="<?= $id ?>" />

Javascript - change input value on click not working

Here's a div which represents cart item qty field:
<div class="cart_quantity_div">
<form action="cart.php" method="post">
<button class="cart_qty_controls" onclick="minusOne(qty_field_<?php echo $item_id; ?>)">-</button>
<input type="text" name="cart_item_qty" value="<?php echo $each_item['quantity']; ?>" size="1" maxlength="3" id="qty_field_<?php echo $item_id; ?>"/>
<input type="submit" name="qty_adjust_cart<?php echo $item_id; ?>" value="change" class="cart_qty_adjust_btn"/>
<input type="hidden" name="cart_item_to_adjust_qty" value="<?php echo $item_id; ?>"/>
</form>
</div>
It is a draft version so don't pay attention to submit and hidden inputs
And JS code:
function minusOne (input_id){
var subtracted_qty = document.getElementById(qty_field_id).value = document.getElementById(qty_field_id).value - 1;
document.getElementById(qty_field_id).value = subtracted_qty;
}
I want to change value of input text field by clicking minus button by -1. But for some reason it's not working.
Can you, please, review my code and find whats wrong with it. Or maybe there is some better ways to do such qty change..
Your code does not set the variable qty_field_id, nor does it use the variable input_id. If input_id is referring to the same value as PHP’s $item_id, then try adding the following line to the top of the JavaScript function:
var qty_field_id = "qty_field_" + input_id;
You shouldn't use inline javascript and you aren't referencing the correct element. Try this instead:
HTML:
<div class="cart_quantity_div">
<form action="cart.php" method="post">
<button class="cart_qty_controls" data-productId="<?php echo $item_id; ?>">-</button>
<input type="text" name="cart_item_qty" value="<?php echo $each_item['quantity']; ?>" size="1" maxlength="3" id="qty_field_<?php echo $item_id; ?>"/>
<input type="submit" name="qty_adjust_cart<?php echo $item_id; ?>" value="change" class="cart_qty_adjust_btn"/>
<input type="hidden" name="cart_item_to_adjust_qty" value="<?php echo $item_id; ?>"/>
</form>
</div>
JS:
[].slice.call(document.getElementsByClassName('cart_qty_controls')).forEach(function(el){
el.addEventListener('click', function(e) {
var id = e.target.getAttribute('data-productId');
document.getElementById('qty_field_' + id).value -= 1;
})
})

SESSION variable slow to update textfield in php

I have a form, contained 3 fields, which when the submit button in submitted,
it will changes the session variable values according to the fields, in this case there are 3 variables. then i echo back the variable onto the fields.
For the first time submit, it stores the value beautifully and display in the fields correctly, the problem is that, when i submit the second time, the values are still the same. after i refresh the page, then the values in the fields are changed.
this is partially the codes i'm using now.
<?php
session_start();?>
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
}
?>
After i refresh(F5), then the value changed. i want it to be, when i clicked the submit button, it will change to the new value.
PHP Code:
<?php
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
echo '<script type="text/javascript">'
, 'jsfunctionToPrintUpdatedValues();'
, '</script>'
;
}
?>
Javascript Sample Code
function jsfunctionToPrintUpdatedValues()
{
/* retrieve the updated session variables in javascript variables */
var acc_main_js = <?php echo $_SESSION['acc_main']?>
var acc_id_js = <?php echo $_SESSION['acc_id']?>
var acc_cat_js = <?php echo $_SESSION['acc_cat']?>
document.getElementById("main").value=acc_main_js;
document.getElementById("id").value=acc_main_js;
document.getElementById("cat").value=acc_main_js;
}
in the input fields you have to write like this
`
Below
`
<input type="text" name="acc1" value="<?php if(isset($_SESSION['acc_main'])) echo $_SESSION['acc_main']" />
Use if(isset($_POST['submit']) != '') before the form. Change your code to this:
<?php
session_start();
if(isset($_POST['submit']) != '')
{
$_SESSION['acc_main'] = $_POST['acc1'];
$_SESSION['acc_id'] = $_POST['acc2'];
$_SESSION['acc_cat'] = $_POST['acc3'];
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
</form>
<?php
}else{
?>
<form name="form1" id="form1" action="">
<input type="text" name="acc1" value="<?php echo $_SESSION['acc_main']" />
<input type="text" name="acc2" value="<?php echo $_SESSION['acc_id']" />
<input type="text" name="acc3" value="<?php echo $_SESSION['acc_cat']" />
<input type="submit" name="submit">
<?php } ?>

how to multiple parameters from javascript to html form

here is the code what i am trying
<script>
var output = "<table id='e' border><tr><td><input type='text' id=/"t"/></td><td><input type='text' id=/"t1/"></td><td><input type='text1' id=/"t2"/></td><td><input type='text1' id=/"t3"/></td><button onclick=del('"+ divId +"')>delete</button></td></tr></table>";
</script>
<body>
<form method=post action="t.php">
<input type="text" id= ??(from script...)
</body>
what shell i do in order to send parameters from script to form one by one in text field? is there any possibilty?
In php:
<?php
if($_POST['submit'])
{
$user = $_POST['user'];
$pass = $_POST['pass'];
//Here you have the text in php variables. You can do whatever you want with them
}
?>
<form method="post" enctype="multipart/form-data" name="form">
Username: <input type="text" name="user" required>
Password: <input type="password" name="pass" required>
</form>
Yes, you can get value from input text:
<input type="text" id"yourIDinput" onchange="YouFunctionJavaScript()"/>
<script>
function YouFunctionJavaScript(){
var textInput = $("#yourIDinput").val(); // Here you get value
alert(textInput); //
}
</script>
It is?

GET or import $var as input value to Form after the page is loaded - onClick Button?

I have a FORM with 2 input fields $first_name and $last_name.
<?
$first_name = get_post_meta($post->ID, 'fname', true);
$last_name = get_post_meta($post->ID, 'lname', true);
$fname_tmp = 'Foo' ; // First Name TEMP
$lname_tmp = 'Bar' ; // Last Name TEMP
?>
<input type="text" value="<? echo $first_name;?>" name="first_name" />
<input type="text" value="<? echo $last_name;?>" name="last_name" />
I want to add a onClick "GET/IMPORT" Button/function in this form. So if a user press this button then inputs field first_name should show Foo and last_name should show Bar
How can I do this? Using PHP?
Many thanks in advance.
I try it. it work. Here is your code.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" >
$(function() {
$(".buttonclass").click(function()
{
var fname_tmp = $("#fname_tmp").val();
if($("#fname_tmp").val()=='') fname_tmp="";
var last_name = $("#lname_tmp").val();
if($("#lname_tmp").val()=='') last_name="";
document.getElementById('first_name').value=fname_tmp;
document.getElementById('last_name').value=last_name;
});
});
</script>
<?php
//$first_name = get_post_meta($post->ID, 'fname', true);
//$last_name = get_post_meta($post->ID, 'lname', true);
$fname_tmp = 'Foo' ; // First Name TEMP
$lname_tmp = 'Bar' ; // Last Name TEMP
?>
<input type="hidden" name="fname_tmp" id="fname_tmp" value="Foo"/>
<input type="hidden" name="lname_tmp" id="lname_tmp" value="Bar"/>
<input type="text" value="<?php echo $first_name;?>" name="first_name" id="first_name"/>
<input type="text" value="<?php echo $last_name;?>" name="last_name" id="last_name"/>
<input type="button" name="mybutton" id="mybutton" value="Click Me" class="buttonclass" />
Have a .php file which will respond to the onclick event triggered in the form.
<?php
$result['fname'] = 'FOO';
$result['lname'] = 'Bar';
echo json_encode($result);
?>
Write a jQuery function to trigger the event and receive response from php
You can add this javascript code anywhere in your page, but between your is recommended.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.button').click(function() {
$.get('/path-to-php-file', function(data) {
result = $.parseJSON(data);
$("input[name='first_name']").val(result.fname);
$("input[name='last_name']").val(result.lname);
});
});
});
</script>
Create a button in your form
<input class="button" type="button" value="Get/Import" />

Categories

Resources