Button to redirect - php

Basically I have a textbox and i want when people paste/write a website inside, and press the button, to be redirected to the site with an extra parameter ("/play/bonus")
<input name="website" id="website" type="text" />
<form method="POST" action=document.getElementById('website') & "/play/bonus">
<input name="pn" value="bonus" type="hidden">
<button id="bonus" class="btn btn-default navbar-element pull-center">
<b>50</b>
Satoshis
</button>

I would use Javascript to do this manually in code rather than making the form POST.
Here's an example for you:
<html>
<head>
<title>JavaScript Form Redirection Example</title>
<script type="text/javascript">
function goToWebsite()
{
var url = document.getElementById("inputWebsite").value;
if (url != null)
{
url += "/play/bonus";
window.location.replace(url);
}
}
</script>
</head>
<body>
<form>
<input type="text" id="inputWebsite" placeholder="Website address"/>
<input type="button" value="Go!" onClick="goToWebsite()"/>
</form>
</body>
</html>
Very simply, what this does is make it so when the button is clicked, it runs the goToWebsite function.
The goToWebsite function:
Gets the value of the URL in the text box
Checks to make sure that the URL is not null
Adds /play/bonus to the end of it
Makes the web browser redirect to that page.

document.getElementById('website')
Will give the object of that element. You need to use .value.
document.getElementById('website').value
To get the value of element.
And there is no need to use the javascript, I think so. If you are using PHP
then use header function of PHP to redirect.
Your HTML:
<form method="POST" action="demo.php">
<input name="website" id="website" type="text" />
<input name="pn" value="bonus" type="hidden">
<input id="bonus" type="submit" class="btn btn-default navbar-element pull-center" Value="50 Satoshis">
</form>
And your demo.php file will be like this:
<?php
$redirect = $_POST['website'];
header("location:".$redirect."/play/bonus");

Related

Input in HTML not connecting to PHP post method

I have an HTML input and button:
<form action="validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<form>
<label></label>
<input id="input" name="InputText" type="text"/>
</form>
</div>
<!-- THE BUTTON ITSELF -->
<input type="button" id="button" name="myButton"><b>Search Archive</b>
</form>
in my validate.php file I have this switch statement:
<?php
switch ($_POST["InputText"])
{
case "someval":
http_header("someaddress.com");
die();
break;
}
?>
the problem is that when I click the button it doesn't do anything. I did this with JS and it worked but it should be noted that I'm really new to web development so if anyone can explain to me what I did wrong and specifically why that would be great. Thanks!
You have a form inside of a form, that won't work. Also, you need to include an <input type="submit" value="submit" /> before you close your form. This is what submits the information from the form to your action="file.php".
A form would typically look like this:
file.html
<form action="validate.php" method="POST">
<input type="text" name="username" placeholder="Enter your username" />
<input type="submit" name="submit" value="Submit" />
</form>
Then you'd do something like this:
validate.php
<?php
echo "Your username is" . $_POST['username'];
The $_POST['username'] is the data gathered from the name="username" input from the HTML. If you write die($_POST); you'll get all the data that is sent through the form.
When you are using type='button' you have to perform the submit by yourself.
So, you can do that using javascript or change to type='submit'.
Example:
<input type="button" id="button" name="myButton"><b>Search Archive</b>
To
<input type="submit" id="button" name="myButton" value="Search Archive" />
you can try this
<form action="/validate.php" method="post">
<!-- THE CODE INSERT -->
<div id="code">
<label></label>
<input id="input" name="InputText" type="text"/>
</div>
<!-- THE BUTTON ITSELF -->
<button type="submit" id="button" name="myButton">Search Archive</button>
</form>
in the div id ="code" you used form tag that's why its not work...delete it will work and button type must be submit

On click submit go to an url from field

I have a simple form:
<form class="form-contact-warp form-calc-ship cb-form" action="javascript:redirect();">
<input class="form-control" required="" id="textBox" type="text" name="code" size="15" placeholder="USI-TECH affiliate ID">
<button type="submit" class="btn-main-color btn-block"> Create page</button>
</form>
If I put in input field the text daniel, I want to append a link.
Example:
I put daniel in and I click submit. I want to appear below the link
www.example.com/daniel and the text This is your link.
Thanks
I think the best way to do this is to use jQuery or JavaScript by itself:
<form class="form-contact-warp form-calc-ship cb-form" action="" method="post">
<input class="form-control" required="" id="textBox" type="text" name="code" size="15" placeholder="USI-TECH affiliate ID">
<button type="submit" class="btn-main-color btn-block"> Create page</button>
</form>
<div id="response"></div>
<!-- add jquery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function(){
$('form').on('submit',function(e){
e.preventDefault();
$('#response').html('This is your link:<br />http://www.example.com/'+encodeURI($('#textBox').val()));
});
});
</script>
Here is a jQuery demo found at jsFiddle
Here is the same thing, only it changes when you type.
If you want to use purely PHP, you need to check something has been submitted:
<form class="form-contact-warp form-calc-ship cb-form" action="" method="post">
<input class="form-control" type="text" name="code" size="15" placeholder="USI-TECH affiliate ID">
<input type="submit" class="btn-main-color btn-block" value="Create page" />
</form>
<?php
# Check if there is a post
if(!empty($_POST['code'])) {
# You want to make sure you remove possible html and covert the string to a url.
echo 'This is your link:<br />http://www.example.com/'.urlencode(trim(strip_tags($_POST['code'])));
}

Submit Only One of Two toggling forms

I have two different forms which are being included in a php file. Their visibility is based on a onClick JS toggle function. The toggle works great. However if I was to fill only one of the forms out and click its respective submit button, I get sent back to the same page rather than the action.php file that i have specifed with this in the broswer:
http://localhost/?user_temp=f&pass_temp1=f&pass_temp2=ff&email_temp=f&answer1=3&submitbtn=Signup+Now
And this Javascript error: "TypeError undefined document.hform.sSecureUser
Both Forms also have their own javascripts to MD5 some data and sSecureUser comes from the Signup Form.
Interestingly, if I was to remove one of the inlcude forms leaving only the Submit form lets say, it would work. It seems that these forms' javascript is clashing with one another :/ ...
I tried this but it didnt work for me since each one of my forms is using java script. PLEASE HELP AND THANKS IN ADVANCE!!!... Let me know if you would like to see any of my forms, javascript, or php files...
Toggle Code:
<div>
Login
<div id="login-div" style="display:none">
<?php include($_SERVER['DOCUMENT_ROOT'].'/forms/login-form.php');?>
</div>
Sign up
<div id="signup-div" style="display:none">
<?php include($_SERVER['DOCUMENT_ROOT'].'/forms/signup-form.php'); ?>
</div>
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block'){
e.style.display = 'none';
}
else{
e.style.display = 'block';
}
}
//-->
</script>
</div>
Login Form:
<div id="hasJavaScript1" style="display:none">
<form name="login">
Username:
<input type="text" name="user_temp" size=32 maxlength=32><br>
Password:
<input type="password" name="pass_temp" size=32 maxlength=32><br>
<input onClick="passResponse(); return false;" type="submit" name="submitbtn" value="Login now">
</form>
<form action="/action/login-action.php" METHOD="POST" name="hform">
<input type="hidden" name="secureuser">
<input type="hidden" name="securepass">
</form>
</div>
<script language="javascript" src="/js/md5.js"></script>
<script language="javascript">
<!--
document.getElementById('hasJavaScript1').style.display = 'block';
function passResponse() {
document.hform.secureuser.value = MD5(document.login.user_temp.value);
document.hform.securepass.value = MD5(document.login.pass_temp.value);
document.login.pass_temp.value = "";
document.hform.submit();
}
// -->
</script>
SignUP Form:
<?php include ($_SERVER['DOCUMENT_ROOT'].'/functions/functions.php');?>
<div id="hasJavaScript" style="display:none">
<form name="signup">
<label>Username</label> <input type="text" name="user_temp" size=32 maxlength=32><span>alphanumeric, no spaces</span><br>
<label>Type password</label> <input type="password" name="pass_temp1" size=32 maxlength=32><span>alphanumeric, 8-12 long</span><br>
<label>Retype password</label> <input type="password" name="pass_temp2" size=32 maxlength=32><br>
<label>Email</label> <input type="text" name="email_temp" size=32 maxlength=32><br>
<label> What is: </label><?php $captchaArray = myCaptcha(); echo $captchaArray['equation'];?><br>
<label>Answer</label><input type="text" name="answer1">
<input onClick="passResponse1(); return false;" type="submit" name="submitbtn" value="Signup Now">
</form>
<form action="/action/signup-action.php" METHOD="POST" name="signup-hform">
<input type="hidden" name="sSecureUser">
<input type="hidden" name="sSecurePass1">
<input type="hidden" name="sSecurePass2">
<input type="hidden" name="secureEmail">
<input type="hidden" name="answer2">
<input type="hidden" name="checker" value=".<?php echo $captchaArray['answer'];?> .">
</form>
</div>
<script language="javascript" src="/js/md5.js"></script>
<script language="javascript">
<!--
document.getElementById('hasJavaScript').style.display = 'block';
function passResponse1() {
document.signup-hform.sSecureUser.value = MD5(document.signup.user_temp.value);
document.signup-hform.sSecurePass1.value = MD5(document.signup.pass_temp1.value);
document.signup.pass_temp1.value = "";
document.signup-hform.sSecurePass2.value = MD5(document.signup.pass_temp2.value);
document.signup.pass_temp2.value = "";
document.signup-hform.secureEmail.value = MD5(document.signup.email_temp.value);
document.signup-hform.answer2.value = document.signup.answer1.value;
document.signup.answer1.value = "";
document.signup-hform.submit();
}
// -->
</script>
One problem I see is that you do not specify any value attributes or values in your hidden fields:
<input type="hidden" name="sSecureUser">
<input type="hidden" name="sSecurePass1">
<input type="hidden" name="sSecurePass2">
<input type="hidden" name="secureEmail">
<input type="hidden" name="answer2">
But then in your JS, you're trying to access the value attribute.
document.hform.sSecureUser.value = ...
Try adding values to those. I would also give your hidden forms ids and use the document.getElementById() syntax instead of the document.hform.sSecureUser syntax.
As far as your form submits, you are suppressing the action of the first form, trying to fill in values for the hidden fields in the second form and then submitting that. Both of your scripts use the same name for the forms "hform". They need to be unique. I would also give them a unique id.
Another possible issue is this line:
<input type="hidden" name="checker" value=".<?php echo $captchaArray['answer'];?> .">
You're using the . for string concatenation, but it's not in an echo/print statement. I think it's supposed to be:
<input type="hidden" name="checker" value="<?php echo $captchaArray['answer'];?>">

Button onClick not redirecting

SO..i have this project that is totally backwards, but basically i have to give a working form that adds a quantity of some item_ID and then redirects to the cart page of a framework i use.
Of course rather than me just skin my framework, and customize its functionality for him. This guy has some hacked up php pages that he "wrote himself" that he insists on hard coding forms into.
anyways, i have a form that submits to my cart.php in the action
<form name="orderform" method="post" action="http://s429036015.onlinehome.us/cart.php" id="orderform">
<input type="hidden" name="mode" value="add" />
<input type="hidden" name="productid" value="140" />
<input type="hidden" name="cat" value="" />
<input type="hidden" name="page" value="" />
Qty <input type="text" id="product_avail" name="amount" />
<button class="button main-button list-button add-to-cart-button" type="submit" title="" onClick="location.href='cart.php'">
<span class="button-right"><span class="button-left">Add to cart</span></span>
</button>
</form>
The above form submits properly, with the quantity entered, session stores the data and the items are in the cart just fine....however, that onclick event on the <button> doesn't work!
I've even tried this
<button class="button main-button list-button add-to-cart-button" type="submit" title="" onClick="GotoPage(); return:true;">
with this script ahead of the form
<script type="text/javascript">
function GotoPage() {
window.location = "http://s429036015.onlinehome.us/cart.php";
}
</script>
anyone have any ideas? driving me nuts...
In PHP, after you process data from form, try this:
header('location: http://s429036015.onlinehome.us/cart.php');

Why is the form not preventing submission?

I have a basic form that I am using as a dummy to test some JavaScript functionality. My goal is to click the submit button and have submission paused until a javascript function is executed, then submit the form after that. the code I currently have is below, but the submission of the form is not prevented, and the hidden field's value is never set. can someone explain why this is happening?
NOTE I am aware of a jQuery method of performing the same functionality. I am specifically trying to solve this problem without using jQuery. If your only answer is "jQuery is better", please do not bother.
<html>
<head>
<script type="text/javascript">
function test(){
document.getElementById("hidden").value="hidden";
var queryString = $('#decisionform').formSerialize();
alert(queryString);
document.getElementById("form").submit();
}
</script>
</head>
<body>
<?php
if ($_POST){
foreach ($_POST as $key=>$value){
echo "<b>$key</b> $value<br>";
}
}
?>
<form action="hello.php" method="post" id="form">
<input name="first" type="text" />
<input name="second" type="text" />
<input name="third" type="text" />
<input name="hidden" type="hidden" />
<input name="submit" type="submit" value="Submit" onclick="test();return false;" />
</form>
</body>
</html>
You don't have an element with the id hidden so document.getElementById("hidden").value="hidden"; will throw an error as you can't set properties on undefined. The script will stop running and never reach return false so the form will submit as normal.
try <form action="hello.php" method="post" id="form" onsubmit="test(); return false;">
to prevent a submit, you actually need to have a return false, in the onsubmit even of the form itself
along with not having an id on the hidden field.
<input name="submit" type="submit" value="Submit" onclick="test();return false;" />
use a regular button in this case because your function is performing the submit.
<input name="submit" type="button" value="Submit" onclick="test()" />
You're missing the id attribute from your input element. Modify <input name="hidden" type="hidden" /> to <input name="hidden" type="hidden" id="hidden" />.
You can use
onclick="return test()"
and just return false at the end of the function.
Add id="hidden" to your input, like this:
<input name="hidden" type="hidden" id="hidden" />

Categories