How can I transfer values of two different veriable on a single submit button click to an another page........using javascript & php

Dani AI

Generated

The thread already shows the two common approaches: points to using form fields on submit, and suggests using sessions. Hidden inputs or normal form fields are fine for passing simple, non-sensitive values in a single submit. Sessions are suitable when values must persist on the server between pages and not be exposed in the browser. Client-side values can be changed, so server-side validation is required for anything important.

When keeping values server-side, store only the keys needed instead of replacing the entire session array; this preserves other session data and avoids accidental loss. Session data should be set on the page that knows the values and read on the target page. An alternative to a full form submit is sending both values with an AJAX POST (FormData or JSON) and letting PHP read them from $_POST or php://input.

<?php
session_start();
$_SESSION['position'] = $row['Position'];   // set on page A

// on page B
session_start();
$position = isset($_SESSION['position']) ? $_SESSION['position'] : '';
echo htmlspecialchars($position, ENT_QUOTES);
?>
// client-side AJAX example (sends two values to target.php)
const fd = new FormData();
fd.append('first', val1);
fd.append('second', val2);
fetch('target.php', { method: 'POST', body: fd })
  .then(res => res.text()).then(console.log);

Quick checklist: session_start() must run before any output; hidden inputs are visible and client-modifiable so never trust them for auth or pricing logic; always validate and sanitize server-side and escape output (htmlspecialchars); use the browser network tab and var_dump($_POST) to debug; add a CSRF token for state-changing POSTs.

Recommended Answers

All 2 Replies

A solution is to store the variable value in a hidden element inside form. For example:
document.getElementById('id').value = id_var;

Then, when the form is submitted the idea it's to create another hidden element using $_POST or $_GET. In javascript, then will be possible to use:
var id_var = document.getElementById('id').value;

I have posted this before, but here it is again:
I have a page that shows job posings and the recordset contains the job title called Position
on my first page I start the session, name the session and give it the value of the recordset like this:

session_start();
$_SESSION= $row_rsposition;


on the next page, I once again start the session and echo it to the screen to test it like so:

session_start();
echo $_SESSION;

you can further assign it to values on the form like this:
(this is a checkbox element)
<input name="Administrative_Support_Assistant" type="checkbox" id="Administrative_Support_Assistant" value="Y" <? if (!$_POST && (!(strcmp($_SESSION, "Administrative Support Assistant")))) { echo "checked=\"checked\"";} ?>/>

for a text box use something like this:
<input name="Position" type="text" id="Position" value=" <? echo $_SESSION;?>"


hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.