How to fix the following form submit code? - javascript

Page URL:
http://website.net/page
Output in address bar after submit:
http://website.net/page?paste=text
I want the result to look like this:
http://website.net/page?text
Without the name of the input appearing
Is there a way to do that?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php
$u = $_SERVER['REQUEST_URI'];
$n = '/paste?\D(.+]?)/';
preg_match($n, $u, $n);
$url = "$n[1]";
echo $url;
?>
<form id="a">
<input type="text" id="paste" name="paste" maxlength="5" size="5" onchange="submit()">
</form>

Try something like this...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php
$u = $_SERVER['REQUEST_URI'];
$n = '/paste?\D(.+]?)/';
preg_match($n, $u, $n);
$url = "$n[1]";
echo $url;
// Check if is set
if (isset($_POST['paste'])) {
// Echo input value
echo $_POST['paste'];
}
// Change method to post
?>
<form id="a" method="POST">
<input type="text" id="paste" name="paste" maxlength="5" size="5" onchange="submit()">
</form>

Related

How to get the value of the image when clickin

How to get the value of the image for us it on $_POST
$url_f = $crawler->filter('#content .galleries_overview .item')->each(function(crawler $node, $i) {
//echo $node->html();
$image = $node->filter('img')->attr('src');
$src = $node->filter('a')->attr('href');
$href = 'https://example.com/'. $src;
?>
<input type="image" name="submit_blue" value="<?php echo $href ?>" src="<?php echo $image ?>">
<?php
});
?>
you may use onclick
<input type="image" onclick="sendme('<?php echo $href ?>')" src="<?php echo $image ?>">
and add this to your page :
<form method="post" id="myform">
<input type="hidden" name="submit_blue" id="submit_blue" value="" >
</form>
<script>
function sendme(x) {
document.getElementById("submit_blue").value = x;
document.getElementById("myform").submit();
}
</script>
that is a javascript solution i use if i do not want to use jquery or something
By the way I don't like using URL as a value

can we send the id to the file itself with submit form

I tried to post id to file itself in php. But it post nothing. This code I tried
//if can it should echo 1 and point 232
<?php
if ($_POST['submit'] == 'winners') {
$a = $_GET['A'];
echo $a;
$point = $_GET['POIN'];
echo $point;
}
?>
<form enctype="multipart/form-data" method="post" style="width:auto;">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input class="process" name="submit" type="submit" id="submit" value="winners" onclick="location.href='reward-pollingx.php?A=1&POIN='+document.getElementById('timespan').innerHTML'">
</form>
You want to GET value inside span in PHP using JavaScript. Try something like this:
<?php
$a = isset( $_GET['A'] ) ? $_GET['A'] : '';
echo $a;
$point = isset( $_GET['POIN'] ) ? $_GET['POIN'] : '';
echo $point;
?>
<form enctype="multipart/form-data" method="post" style="width:auto;">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input
class="process"
name="submit"
type="submit"
id="submit"
value="winners"
onclick="window.location='reward-pollingx.php?A=1&POIN='+document.getElementById('timespan').innerHTML;return false;"
>
</form>
If you try to pass parameters like a query string you do not need to use a form.
Because you configure your form to send a post request, you can pass the parameters of the query string to input values of hidden type. Test as follows
<form action="reward-pollingx.php" enctype="multipart/form-data" method="post" style="width:auto;">
<input type="hidden" name="A" value="1">
<input type="hidden" name="POIN" value="">
<input type="hidden" name="submit" value="winners">
<div class="box">
<span class='odometer' id="timespan" name="timespan">232</span>
</div>
<input class="process" name="submit" type="submit" id="submit" value="winners">
</form>
<script>
document.querySelector('input[name=POIN]').value = document.getElementById('timespan').innerHTML;
</script>
Your php code should change to use $_POST instead of $_GET
<?php
if ($_POST['submit'] === 'winners') {
$a = $_POST['A'];
echo $a;
$point = $_POST['POIN'];
echo $point;
}
Or use query string to pass parameters to the PHP script
Submit
And the php
if ($_GET['submit'] === 'winners') {
$a = $_GET['A'];
echo $a;
$point = $_GET['POIN'];
echo $point;
}
To build the query string because you need to get information from the view you probably need to build it dynamically
If you need to submit the form to itself, you can use
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" ....
or you can use:
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST" ....

js submit not works not sending post form data to php

After certain time (2min) when i try form submit using javascript it just submit form without post data value !!! searched solution on web but unable to find solution!! Hope someone can help me
Code:
<script type="text/javascript">
$(function() {
$('.countdown.callback').countdown({
date: +(new Date) + 120000,
render: function(data) {
$(this.el).text( this.leadingZeros(data.min, 2) + "min " + this.leadingZeros(data.sec, 2) + " sec");
if(this.leadingZeros(data.min, 2) < 01 && this.leadingZeros(data.sec, 2) <= 10){
$(this.el).addClass('ended');
}
},
onEnd: function() {
document.getElementById('myform').submit();
}
});
});
</script>
html code with php
<?php
foreach($row as $r){
if(!empty($r['question'])){
?>
<form method="post" id="myform" action="subanswer.php" >
<p><span style="color:rgb(33,79,157);"><?php echo $r['id'];?> <?php echo $r['question'];?></span><br />
<input type="radio" name="<?php echo $r['test_id'].'_'.$r['id'];?>" value="<?php echo $r['ans1'];?>"> <?php echo $r['ans1'];?>
<br>
<input type="radio" name="<?php echo $r['test_id'].'_'.$r['id'];?>" value="<?php echo $r['ans2'];?>"> <?php echo $r['ans2'];?>
<br>
<input type="radio" name="<?php echo $r['test_id'].'_'.$r['id'];?>" value="<?php echo $r['ans3'];?>"> <?php echo $r['ans3'];?>
<input type="hidden" name="testname" value="<?php echo $_GET['id']?>">
</p>
<?php
}
}
?>
<input type="submit" name="subans" value="add" class="but" style="width:15%;" />
</form>
From your code, it looks like you're calling the wrong form:
document.getElementById('myForm').submit();
The ID myFom does not exist. You have:
<form method="post" id="myform1" action="subanswer.php" >
Also not sure why people don't stick to JQuery throughout. Would advise:
$("#myForm1").submit();

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 } ?>

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