unable to get post data in php from data generated by javascript - php

<script>
function funcGet(val)
{
if(val=="Create new category")
{
var element = document.createElement("input");
element.setAttribute("type", "input");
element.setAttribute("name", "new_cat");
element.setAttribute("placeholder", "Enter a new category name");
var foo = document.getElementById("cat");
foo.appendChild(element);
}
}
</script>
<div id="cat">
<form method="post" action="">
<input type="text" placeholder="Enter a topic name" name="word"><br/><br/><br/>
<input type="input" name="link" placeholder="Enter a link here"><br/><br/><br/>
<select name ="category" onchange="funcGet(this.value);">
<?php
displayCat();
?>
</select>
<br/><br/>
</div>
<select name="site_type">
<?php
displaySitetype();
?>
</select><br/><br/><br/>
<input type="submit" value="Submit data">
</form>
<?php
if($_POST['category']=="Create new category") {
if($_POST['new_cat'])
{
$new_cat=mysql_real_escape_string($_POST['new_cat']);
create_new_catentry($new_cat);
$catname=$new_cat;
}
?>
I am not able to get the value of new_cat using $_POST in the same page. What am i doing wrong? I am not able to solve it. I am able to get every other post data from the page without any fault, is there any naming stuff gone wrong? I am not sure what the problem is, is my Javascript wrong?
Update: Please note that the php script down at the bottom is just a small part of a system not the whole part.

In your JS code you're appending the new <input> within the cat div, but this way it will be outside the <form>. Append your field to the form, assigning it an id, like this:
HTML:
<form method="post" action="" id="catform">
JS:
var foo = document.getElementById("catform");
foo.appendChild(element);

In your javascript add new attribute:
element.setAttribute("id", "new_cat");

Related

Call php using arguments from a text box

I want to make a website that has a text box and if an user enters "hello" and clicks submit I want to show example.com/test.php?link=hello
How can I do that? The php is already written I just need to make something with html or php to be able to show example.com/test.php?link=whatUserEnters
Thanks!
The form should have the method "get" defined. Then on the test.php page you just
<form method="get" action="test.php">
...
<input type="text" name="link"/>
</form>
and on the test.php
<? echo $_GET['link']; ?>
or
<?= $_GET['link']?>
Edit with the POST solution:
<form method="post" action="test.php">
...
<input type="text" name="link"/>
</form>
and on the test.php
<? echo $_POST['link']; ?>
<!DOCTYPE html>
<html>
<body>
<!--just create a text box, give an id to it.-->
<input type="text" id="text_to_display" />
<input type="button" value="Submit" onClick="concat();" />
<!--Create a label or div assign a separate id to it too.-->
<p id="concatenated_text"></p>
<script>
//Create a function in javascript.
function concat() {
//Get the value in function using that id.
var text = document.getElementById("text_to_display").value;
//Concatenate the value with the string and put it in div using id.
var final_text = "example.com/test.php?link="+text;
// you can use location.href to use it for link purpose
location.href = final_text;
}
</script>
</body>
</html>

Post Form Action to variable inside form

I have the following form. I want to change the form action to include the variables inside this form, 500pxuserid. Is there a way to do this? For the user experience, I want to prevent a re-direct. Currently, I get the variable on the other side, and then re-route them a new url, but this required a re-direct.
<form action="importphotos.php?view=500&mode=upload&page=1&u=&u2=&500pxuserid=" method="post">
<div style="width:100%;height:45px;background-color:rgb(2,173,234);">
<img style="width:30px;padding:10px;float:left;" src="https://photorankr.com/graphics/import_500pxicon.png" />
<h3 style="font-weight:300;font-size:20px;text-align:left;padding-left:40px;margin-top:8px;color:white;float:left;">Login to 500px</h3>
</div>
<div style="width:90%;height:150px;border-bottom:1px solid rgba(255,255,255);float:left;margin-top:-5px;">
<input style="outline:none;padding:8px;border-radius:3px;font-size:18px;width:263px;float:left;border:1px rgb(100,100,100);border-style:solid; margin:8px; margin-left:16px;" placeholder = "Enter your 500px id" type="text" name="500pxuserid" />
<input type="password" style="outline:none;padding:8px;border-radius:3px;font-size:18px;width:263px;float:left;border:1px rgb(100,100,100);border-style:solid; margin:8px; margin-left:16px;" placeholder = "Enter your 500px password" type="text" name="500pxuserpassword" />
<input class = "submitbox" type="submit">
</div>
</form>
</div>
Using javascript you could change the form action on form submit (onsubmit).
<script type="text/javascript">
function get_action(form) {
var user = document.getElementById('userid').value;
form.action = "importphotos.php?view=500&mode=upload&page=1&u=&u2=&500pxuserid=" + user;
}
</script>
<form action="" method="post" onsubmit="get_action(this);">
...
// add id=""userid" to input
<input ... type="text" name="500pxuserid" id="userid" />
</form>
Three things:
Add an id attribute to your form. For example, foo.
Add an onsubmit attribute to your form, onsubmit="foo()"
Add an id attribute to your 500pxuserid input. For example, bar.
It should look like this:
<form id="foo" onsubmit="foo();" ...
And your input:
<input id="bar" ...
And in your JavaScript add the following function:
function foo()
{
var form = document.getElementById('foo'),
bar = document.getElementById('bar');
form.action = 'importphotos.php?view=500&mode=upload&page=1&u=&u2=&500pxuserid=' + bar.value;
}
Also, keep in mind that this would only work if the user's browser has JavaScript enabled.

Hide form with php on submit?

I have a simple php - password protecton / login based on a form which works. PHP-code is in page "secure.php" and includes the html-file "accessed.html" when user + pass is correct.
But i want the form to hide when hidden page (accessed.html) is shown.
I have tried wrapping the form in a div and using javascript and display: none to hide, but it doesnt work - not locally or on server.
What am i doing wrong? It doesnt have to be js hiding the form after login..
PHP in top
<?php
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "a"
&& $pass == "a")
{
include("accessed.html");
echo "<script>
document.getElementById('wrap').style.display = 'none';
</script>";
}
if(isset($_POST))
?>
And the form in the body:
<div id="wrap">
<form method="POST" action="secure.php">
User <input type="text" name="user"></input>
Pass <input type="text" name="pass"></input>
<input type="submit" name="submit" value="access page"></input>
</form>
</div>
Move the <script> to after wherever you display "#wrap"
If you want to do it on the server side (PHP) just use an if statement.
I think this is what you are looking for my friend
<html>
<body>
<div id="wrap">
<form method="POST" action="temp.php" target = "hidden">
<input type="text" name="user">User</input>
<input type="text" name="pass">Pass</input>
<input type="submit" name="submit" value="access page" onclick = "closediv()"></input>
</form>
</div>
<iframe name = "hidden" id = "hidden" style = "display: none"></iframe>
<script>
function closediv(){
document.getElementById("wrap").style.display = "none";
}
</script>
</body>
</html>
With the above code, you'll need to do your PHP processing on the current page. If this isn't doing what you want it to do and I misread your question, let me know and we can work on your predicament.

how to submit div in a form

I am using ckeditor in order to edit the text seen in the screen. The information is taken from database and written to the screen in a div element and it is possible to edit the information by double clicking. However, after edited i couldn't get the edited information. Here is my code, i tried to add a form which includes the div element, but it did not work.
<form method="post">
<p>
Double-click any of the following <code><div></code> elements to transform them into
editor instances.</p>
<?php
$makeleSql = 'SELECT * FROM makale';
$makaleRs = $con->func_query($makeleSql);
while ($makaleRow = mysql_fetch_array($makaleRs)) {
?>
<div class = "editable" id = <?php echo "content".$makaleRow['id'];?> style="display:none">
<?php
$contentSql = 'SELECT * FROM content WHERE makale_id ='.$makaleRow['id'];
$contentRs = $con->func_query($contentSql);
while ($contentRow = mysql_fetch_array($contentRs)) {
echo $contentRow['icerik'].$makaleRow['id'];
}
?>
</div>
<?php }
?>
<button type="submit" value="Submit" onclick="getDiv();"/>
</form>
What should i do in order to take the information in the div element? Moreover, i am using this example.
http://nightly.ckeditor.com/7484/_samples/divreplace.html
Thanks.
For save form's data you need store information in input/select/textarea. Div and other not form's element not will be stored.
You have to store your data in hidden fields:
<form method="post">
<p>
Double-click any of the following <code><div></code> elements to transform them into
editor instances.</p>
<?php
$makeleSql = 'SELECT * FROM makale';
$makaleRs = $con->func_query($makeleSql);
while ($makaleRow = mysql_fetch_array($makaleRs)) {
?>
<div class="editable" id="<?php echo "content".$makaleRow['id'];?>">
<?php
$contentSql = 'SELECT * FROM content WHERE makale_id ='.$makaleRow['id'];
$contentRs = $con->func_query($contentSql);
while ($contentRow = mysql_fetch_array($contentRs)) {
echo $contentRow['icerik'].$makaleRow['id'];
// store original text
echo '<input type="hidden" name="'.$makaleRow['id'].'" value="'.htmlspecialchars($contentRow['icerik'].$makaleRow['id']).'">;
}
?>
</div>
<?php }
?>
<button type="submit" value="Submit" onclick="getDiv(this);"/>
</form>
<script>
var getDiv = function(btn) {
for(var el in btn.form.elements) {
var d = document.getElementById(btn.form.elements[el].name);
btn.form.elements[el].value = d.innerHTML;
}
return true;
}
</script>
Generically adds <input type="hidden"> to any <div> with the attribute addToForm, copies content into them (see notes below):
<form method="post" id="f">
<div id="txt" addToForm="1" contenteditable spellcheck="true" style="height:100px;width:300px;font-family:arial,sans serif;border:1px solid black;font-weight:normal;overflow-y:auto;"
</div><br />
<input type="button" value="Go" id="btnGo">
</form>
<script type="text/javascript">
$(document).ready(function(){
$("#btnGo").click(function(){
$("div[addToForm]").each(function(){
var tId=$(this).prop("id");
$(this).after("<input type='hidden' name='"+tId+"' id='hdn"+tId+"'>");
$("#hdn"+tId).val($(this).html());
});
$("#f").submit();
});
});
</script>
Note 1) if you want to strip out the HTML formatting of the content, use <textarea> instead of <input>
Note 2) be sure validation is successfully complete first or you will end up with multiple hidden inputs with the same name

PHP Pass variable to popup form within same page

Following on from a previous question, (previous question here), the problem I'm having seems to involve trying to pass/post a value through a form when the form action is '#'. I've tried session data but it always returns the last item from the database. Everthing else returns nothing.
Any help/ideas/advice greatly received, S. (Code below)
This is the code that displays the list of items, each containing an 'email' link/button to one instance of a popup window/form that is located at the bottom of the page.
<?php
$query = mysql_query("select * from istable where categoryID = '1'");
while ($result = mysql_fetch_array($query)) {
echo '<h4>'.$result['title'].'</h4>
<p>'.substr($result['descrip'],0,408).'... <strong>Read more</strong></p>
<form action="#" method="post" rel="#sheet" class="see">
<input type="hidden" name="propTitle" value="'.$propResult['title'].'">
<input type="submit" name="submit" value="Email">
</form>
';
}
?>
This is the code for the popup window/form at the bottom of the same page that is called through jquery.
<div id="sheet" class="rounded">
<!--{{{ pane1 -->
<div class="pane" id="pane1">
<h4>Email Details to a Friend</h4>
<p>You have selected to forward the details of <?php echo $_POST['propTitle']; ?> to a friend.</p>
<p>Please fill out the following form</p>
<form class="rounded" id="email-form" method="post" action="<?php echo $pageLink; ?>">
<!-- form goes in here -->
</form>
</div>
<!--}}}-->
</div>
<script type="text/javascript">
$(".see").overlay({mask: '#999', fixed: false}).bind("onBeforeClose", function(e) {
$(".error").hide();
});
</script>
Why are you using PHP for this? If the popup is called through the same page, use JavaScript to get the DOM element value and if you need to process data use AJAX.

Categories