pass variable value in action url php - javascript

How to pass variable values into url. Here is my code
<?php
if(isset($_POST['submit'])){
echo $v=$_POST['n'];
} ?>
<form method="post" action="/user/<?php echo $v; ?>">
<input type="text" name="n">
<input type="submit" name="submit" value="Submit">
After giving input value and click on submit shows in url:
if input value is raj after click on submit in url i want to display
/user/raj

The requirement can be achieved by using javascript.
<form id="myForm" method="post" action="" onsubmit="return false">
<input type="text" name="n">
<input type="submit" name="submit" value="Submit">
</form>
using jquery
$("#myForm").on("submit",function(){
var val=$("input[name='n']").val();
$(this).attr("action","<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>/user/"+val);
$(this).submit();
})

Use method="get" in your form tag.

Related

Submit 2 input with 1 button

I need to load 2 actions with 1 button. The first action uploads a file to the server and the second action loads some customer data.
HTML
<p class="text-center font-big"><u>Load Photometric Data (.ies File)</u></p>
<form id= "form1" method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="hidden" name="upload_file" />
</form>
<form id= "form2" method="post" action="/roadlite_main.php">
<input type="hidden" name="new_data" />
</form>
<input type="button" value="Click Me!" onclick="submitForms()" />
js
<script language="javascript">
submitForms = function()
{
document.getElementById("form1").submit();
document.getElementById("form2").submit();
}
</script>
The second form (or maybe the last) always loads OK ... the first form fails to load and does not upload the file.
Any ideas!

How do you pass a variable into a html submit and a php isset to allow them to match up?

I am very new to PHP and don't know how to match the name of the submit button to the argument inside the $_POST. $email is a string variable. I echo it to make it be the name of the input button and I also set it as the argument of $_POST. But this does not work. Can anybody tell me why?
<tr>
<form method="post" action="">
<input type="submit" name="<?php echo $email?>" value="Approve" class="btn btn-primary">
</form>
</tr>
<?php
if(isset($_POST[$email])) {
echo "z";
}
?>
You need <input type="hidden"> with name="email" and value="PHP variable value" with submit button name="action" with value="Approve"
<form method="post" action="">
<input type="submit" name="action" value="Approve" class="btn btn-primary">
<input type="hidden" name="email" value="<?php echo $email; ?>"/>
</form>
Then after posting it you can just check for the action and email
<?php
if (#$_POST['action'] && $_POST['email']) {
if ($_POST['action'] == 'Approve') {
echo $_POST['email'];
}
}
?>

How to submit POST requests if form is a value of innerHTML?

Every time I access $_POST on the other page, there is no value. How do we submit post request if the form is in innerHTML?
function edit(firstname){
var firstname_old = document.getElementById("firstname-div")
//Set id="FirstName" to your input field
firstname_old.innerHTML= 'First Name:<input type="text" id="FirstName" name="firstname" value="'+ firstname +'">';
}
<form class="" action="summary.php" method="post">
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<button type="button" onclick="edit('<?php echo $firstname ?>')">edit</button><br>
<input type="submit" name="submit" value="submit">
</form>
When you are not in "edit-mode" your're putting out just a text.
<?php echo "First Name: $firstname<br>"; ?>
Thist text is inside the form which will be submitted but its not a form-field! To achieve what you want (deliver the content of "firstname" via ajax) you need a "hidden field".
So you put the same value which is shown in the text to a hidden input like this.
<form class="" action="summary.php" method="post">
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<button type="button" onclick="edit('<?php echo $firstname ?>')">edit</button><br>
<input type="hidden" name="firstname" value="'<?php echo $firstname ?>'">
<input type="submit" name="submit" value="submit">
</form>
What I understand that you want to display a textbox to edit the name on clicking the edit button . Here is one solution for that:-
<script>
$(document).ready(function(){
$("#edit").click(function(){
document.getElementById("firstname-div").style.display = "none" ;
document.getElementById("firstname-div-edit").style.display = "block" ;
});
});
</script>
<HTML>
<form class="" action="summary.php" method="post">
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<div id="firstname-div-edit" style="display:none;">
<input type="text" name="firstname" value="'<?php echo $firstname ?>'">
</div>
<button type="button" id="edit">edit</button><br>
<input type="submit" name="submit" value="submit">
</form>
</HTML>
Check and let me know if it works for you or not.

Show values on POST

Working on a really simple form for a district site. I have a really simple form in PHP.
<form method="post">
<fieldset>
<legend>Enter Age and Weight</legend>
<label>Age:</label>
<input type="text" name="age" value="<?php echo #$_POST['age'] ?>">
<label>weight:</label>
<input type="text" name="weight">
<div>
<button class="btn" type="submit" name="action" value="enter">Enter</button>
</div>
</fieldset>
</form>
What I am trying to do is when the user presses the enter button, I want to alert the user of what they have entered.
This is what I have so far in my HTML.
<body onload="document.forms[0].submit()">
<form action="/index.php" onsubmit="" method="POST">
<script>
alert(document.getElementsByName("age").value);
</script>
</form>
</body>
However, I keep seeing "undefined". I am assuming that is happening because on page load my script is being run instead of when the user presses the submit button. Kind of confused how to just do a simple alert. Appreciate any help.
You must insert your code in a function that you must attach to a event handler like onsubmit, something like this:
HTML:
<form method="post" onsubmit="showData();">
JAVASCRIPT:
function showData() {
alert(document.getElementsByName("age")[0].value);
}
I've inserted [0] in your Javascript code because document.getElementsByName returns you an Array of elements, in your case, this Array, obviously contains only one value that is retrievable on the index 0 (first index of any array).
<form method="post" id="myform">
<fieldset>
<legend>Enter Age and Weight</legend>
<label>Age:</label>
<input type="text" name="age" value="<?php echo #$_POST['age'] ?>" id="age">
<label>weight:</label>
<input type="text" name="weight">
<div>
<button class="btn" type="button" name="action" value="enter" onclick="alert(document.getElementById('age').value);document.getElementById('myform').submit()">Enter</button>
</div>
</fieldset>
</form>
Is this what you want?

Using link instead of submit input to submit form in JQuery

I have a form with a submit button:
<form method="post" action="" encrypt="multipart/form-data">
<input type="submit" name="delete" value="Delete">
</form>
I'd like to change the button to an a href:
<form method="post" action="" encrypt="multipart/form-data">
Delete
</form>
However, I dont know how to attribute the name="delete" to it
The input type="submit" actually post you form. but if you use anchor instead of "submit" button, then you have to do something else, since it will not post your form.In this case you can use a trick.
<form method="post" action="" encrypt="multipart/form-data">
<input type="submit" style="display:none;" name="delete" value="Delete">
Delete
</form>
This should work.
Just bind to the click event of your link.
Delete
$('#deleteLink').click(function(evt) {
$.ajax({
url: "/delete"
method: "post",
data:
}
}
<form method="post" action="" encrypt="multipart/form-data">
<input type="hidden" name="delete" value="Delete">
Delete
</form>
Just type hidden your input.
do like this:
<form method="post" action="" encrypt="multipart/form-data">
<input type="submit" name="delete" value="Delete" style="display:none;">
Delete
</form>
Jquery:
$('linkDel').click(function(){
$(this).closest('input[type="submit"]').click();
})

Categories

Resources